Sails:如何从find函数返回一个值

时间:2015-08-21 11:21:23

标签: javascript node.js return sails.js waterline

我在sails js工作。我想返回变量sequence[0].NextValue;。但我没有得到它。

function AutoGenerate(tablename) 
    {
        Sequence.find({TableName:tablename}).exec(function(err,sequence){
            if(err)
            {
                console.log("err");
                return res.negotiate(err);

            }
            else
            {
                console.log(sequence);
                var intCurrentNo = sequence[0].NextValue;
                var intNextNo = sequence[0].NextValue + sequence[0].IncrementBy;                    
                if (intNextNo < sequence[0].MinimumValue || intNextNo > sequence[0].MaximumValue)
                {
                    console.log("Error While Updating UserId")
                    return res.badRequest('Next value not between the Minimum and Maximum value');

                }
                else
                {
                    sequence[0].NextValue = intNextNo;
                    console.log(intNextNo);
                    sequence[0].save(function(err)
                        {
                            if (err) 
                            {
                                console.log("error while update");
                                return res.negotiate(err);
                            }
                            else    
                            {
                                console.log("Incremented");
                                console.log(sequence[0].NextValue); 
                                return sequence[0].NextValue; 
                            }
                        });


                    }
                }
        });
    }

但是Sequence.find({TableName:tablename})函数没有返回任何值。请帮我摆脱这个。

1 个答案:

答案 0 :(得分:4)

您无法返回值,因为它是一种异步方法,您必须使用回调来执行此操作:

function AutoGenerate(tablename, callback)
{
  Sequence.find({TableName : tablename}).exec(function (err, sequence)
  {

    if (err)
    {
      console.log("err");
      callback(err);

    }
    else
    {
      console.log(sequence);

      var intCurrentNo = sequence[0].NextValue;
      var intNextNo    = sequence[0].NextValue + sequence[0].IncrementBy;

      if (intNextNo < sequence[0].MinimumValue || intNextNo > sequence[0].MaximumValue)
      {
        console.log("Error While Updating UserId")
        callback(new Error('Next value not between the Minimum and Maximum value'));

      }
      else
      {

        sequence[0].NextValue = intNextNo;
        console.log(intNextNo);
        sequence[0].save(function (err)
        {
          if (err)
          {
            console.log("error while update");
            callback(err);
          }
          else
          {
            console.log("Incremented");
            console.log(sequence[0].NextValue);
            callback(null, sequence[0].NextValue);

          }
        });


      }
    }


  });
}

并称之为:

AutoGenerate("myTableName", function(err, nextValue){
    if(err){res.negotiate(err);}
    else {/* DO WHAT YOU WANT */}
});