函数没有从nodejs app中的简单函数传递正确的返回值

时间:2012-08-06 00:05:17

标签: node.js javascript

我有以下代码,并尝试使用node.js创建一个Turntable bot。这段代码说当用户键入" q +"我们必须确保它尚未在队列中,它还没有DJ,如果它满足这两个要求,则将它们添加到队列中。否则,如果它不符合前2个标准之一,请告诉用户并且不要触摸队列。

我的问题是" isCurrentDJ(userId)"。当我通过该函数传递userId时,该函数给出了正确的答案。但是,函数总是传回" false"即使答案是真实的"并且isCurrentDJ(userId)函数中的console.log()函数证明了这一点。

我不是最精通js的人,所以我认为这可能是一个可变范围的问题。但我真的不确定,并且已经挣扎了几个小时!任何帮助将不胜感激。谢谢!

// When someone speaks, listen to see if it is one of the q commands
bot.on('speak', function (data) {
var name = data.name;
var text = data.text;
var userId = data.userid;

// q+ :: Add to Queue
if (text.match(/^q\+$/)) {

  //Index is the position in the queue that this person's name is found.
  //If its not found, -1 is returned.
  var index = queue.indexOf(name);

  //Function to determine if the user is currently a DJ
    function isCurrentDJ(user_id, callback){
      bot.roomInfo(false, function (data) {
        var djList = data.room.metadata.djs;
        for (i = 0; i < djList.length; i++){
          if (djList[i] == user_id){
            console.log('recognized as a DJ'); //Consistently printed!
            callback(true);
          }
        }
        callback(false);
      });
    }


  isCurrentDJ(userId, function(isDJ) {
    //If the user is already in the queue
    if(index > -1){
      //Tell them they are already in there
      bot.speak('You are already on the list');
    } else if(isDJ){
      //Otherwise if they are already a DJ tell them that
      bot.speak('You are already a DJ, '+name);
    }else{
      //Otherise if they are not in the queue add user to end of queue
      queue.push(name);
      //Tell them about it and the updated q
      bot.speak(name+' has been added to queue.');
    }
  });

}

2 个答案:

答案 0 :(得分:1)

您的问题是bot.roomInfo是一个异步函数。

当你调用它时,它立即返回,currDJ仍然是假的。不久之后,调用了回调(function(data) {...)。 node.js的大多数API都是异步的,因此您的代码永远不会阻塞。

以下是重写代码的方法:

// When someone speaks, listen to see if it is one of the q commands
bot.on('speak', function (data) {
   var name = data.name;
   var text = data.text;
   var userId = data.userid;

   // q+ :: Add to Queue
  if (text.match(/^q\+$/)) {

  //Index is the position in the queue that this person's name is found.
  //If its not found, -1 is returned.
  var index = queue.indexOf(name);

  //Function to determine if the user is currently a DJ
    function testCurrentDJ(user_id, cb){

      bot.roomInfo(false, function (data) {
        var djList = data.room.metadata.djs;
        for (i = 0; i < djList.length; i++){
          if (djList[i] == user_id){
            console.log('recognized as a DJ'); //Consistently printed!
            return cb(true);
          }
        }

        cb(false);
      });
    }

  //If the user is already in the queue
  if(index > -1){
    //Tell them they are already in there
    bot.speak('You are already on the list');
    return;
  }

  testCurrentDJ(userId, function(isDJ) {
      //Otherwise if they are already a DJ tell them that
      if(isDJ) {
        bot.speak('You are already a DJ, '+name);
      } else {
       //Otherise if they are not in the queue add user to end of queue
       queue.push(name);
       //Tell them about it and the updated q
       bot.speak(name+' has been added to queue. Is Current DJ? '+isDJ);
  }

    })
}

我刚刚更新了您的代码,以向您展示基本想法。在node.js的API中,回调的第一个参数通常是一个错误对象,如果一切正常,则为null。

答案 1 :(得分:0)

bot.roomInfo可能是异步函数吗?如果是这样,currDJ的值将设置为true,但为时已晚,因为您已经返回了它。你不能对currDJ的值进行操作,直到调用回调为止。

您对异步函数的概念有多熟悉?