1

我是'nodejs'世界的新手。所以想要探索各种技术,涉及的框架我正在构建一个由redis支持的简单用户帖子系统(用户发布其他人看到帖子的内容)。我正在使用推荐的express框架大多数教程。但是我在从redis服务器获取数据时遇到了一些困难,我需要从redis服务器进行3次查询才能显示帖子。在这种情况下,每次redis调用后都必须使用neested回调。所以我想使用streamline .js 来简化回调。但即使在我使用之后npm install streamline -grequire('streamline').register();调用之前我也无法让它工作

var keys=['comments','timestamp','id'];
var posts=[];
for(var key in keys){
    var post=client.sort("posts",'by','nosort',"get","POST:*->"+keys[key],_);
    posts.push(post);
}

我收到错误ReferenceError: _ is not defined

请指出我正确的方向或指出我可能错过的任何资源。

4

1 回答 1

1

The require('streamline').register() call should be in the file that starts your application (with a .js extension). The streamline code should be in another file with a ._js extension, which is required by the main script.

Streamline only allows you to have async calls (calls with _ argument) at the top level in a main script. Here, your streamline code is in a module required by the main script. So you need to put it inside a function. Something like:

exports.myFunction = function(_) {
  var keys=['comments','timestamp','id'];
  var posts=[];
  for(var key in keys){
    var post=client.sort("posts",'by','nosort',"get","POST:*->"+keys[key],_);
    posts.push(post);
 }
}

This is because require is synchronous. So you cannot put asynchronous code at the top level of a script which is required by another script.

于 2013-05-10T07:09:47.877 回答