0

目前遇到一个非常令人沮丧的问题。

我可以进行 Meteor.http.get 调用,但是当我把它放在一个循环中时,就会发生不好的事情。

我不是 100% 确定我在做什么,所以任何指针都会很棒!

此代码有效 - 它是咖啡脚本

Meteor.http.get("http://some_site.com/feed.rss", (error, results)->
  console.log "results: " + results.statusCode 
  if results.statusCode is 200
    console.log results.content
)

此代码崩溃:

things.forEach( (thing) ->
  console.log thing.url  # it logs http://some_site.com/feed.rss
  Meteor.http.get(thing.url, (error, results) ->
    if results.statusCode is 200
      console.log results.content
  )
))

出现此错误:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
TypeError: Cannot read property '_meteor_dynamics' of undefined
at Object.bindEnvironment (app/packages/meteor/dynamics_nodejs.js:48:44)
at Object.call (app/packages/http/httpcall_server.js:72:25)
at Object.get (app/packages/http/httpcall_common.js:40:29)
at app/craftcandy.coffee.js:112:28
at app/packages/mongo-livedata/mongo_driver.js:307:7
at /usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/cursor.js:173:11
at [object Object].nextObject (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/cursor.js:485:5)
at Object.callback (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/cursor.js:478:12)
at Array.0 (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/connection/server.js:197:36)
at EventEmitter._tickCallback (node.js:192:41)

退出并显示代码:1 您的应用程序正在崩溃。等待文件更改。

4

2 回答 2

0

我认为这可能是因为meteor 在底层使用了node.js,而node.js 是异步且非阻塞的。

因此,与其检查 results.statusCode,不如放入回调(请原谅我的咖啡脚本):

things.forEach( (thing) ->
   console.log thing.url  # it logs http://some_site.com/feed.rss
   Meteor.http.get(thing.url, (error, results) ->
      results.on('data', (d) ->
        //Do something here with the data stream
      )

      results.on('end', (rslts) ->
         //Do something here with the results stream, like log to console:
         console.log rslts.content
      )
   )
)

如果脚本语法错误,请提前道歉。正如我所说,我不会说咖啡脚本(还)。

但你明白了要点。我从一个node.js 问题中蚕食了这个。让我知道它是否有效。

于 2012-04-26T16:24:39.053 回答
0

我将怀疑您的代码有问题,我看不到,或者您的 URL 格式错误。您可以尝试像这样测试它:

Things.find().forEach(function(thing){
  var url = thing.url;
  console.log("geting " + url);
  (function (_url){
    try{
      Meteor.http.get(_url, function(error, results){
        if(results.statusCode === 200){
          console.log("request success: " + _url);
        } else {
          console.log("request error: " + _url);
        }
      });
    } catch (e) {
      // if it gets here you probly have a malformed url
      console.log("meteor error!: "+_url);
    }
  })(url);
});

这是我将其转换为咖啡条的未经测试的尝试:

Things.find().forEach (thing) ->
  url = thing.url
  console.log "geting: " + url
  go = (_url) ->
    try
      Meteor.http.get _url, (error, results) ->
        if results.statusCode is 200
          console.log "request success: " + _url
        else 
          console.log "request error: " + _url

    catch e
      # if it gets here you probly have a malformed url
      console.log "meteor error!: "+ _url

  go url
于 2012-04-26T16:53:06.390 回答