【问题标题】:Node.js Chaining promises using q.jsNode.js 使用 q.js 链接承诺
【发布时间】:2016-11-06 01:11:08
【问题描述】:

我将 node.js 与 mongodb 和 q.js 一起用于 promise。

以下是我的 mongodb 架构:

{
  UserId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  },
  FirstName: String,
  LastName: String,
  Gender: String,
  userDocument: [userDocumentSchema],
  userEducation: [userEducationDetailsSchema]
};
var userEducationDetailsSchema = new Schema({
  DegreeName: String,
  University: String,
});
var userDocumentSchema = new mongoose.Schema({
  DocumentName: String,
  DocumentPath: String
});


I have following function by chaining promises:

var updateUserDetails = function (req, res) {
  var finalResult = [];
  /* This is the function to update user using promise.*/
  updateUserBasicDetails(req).then(function (req) {
    _.each(req.body.userEducation, function (sd) {
      return getEducationDetail(req.body._id, sd) /* this fun finds if education details are present or not by iterating usertEducation Array */
        .then(function (result) {
          if (result) {
            /* if education details exist then this will update the details */
            return updateEducationDetails(sd, req.body._id).then(
              function (result) {
                return result;
              })
          } else {
            /*else add the education details*/
            return addEducationDetails(sd, req.body._id).then(
              function (result) {
                return result;
              })
          }
        }).then(function (result) {
          finalResult.push(result);
        })
    })
    return JSON.stringify(finalResult);
  }).then(function (finalResult) {
    res.json({
      "token": finalResult /*but here m getting empty result */
    })
  }).catch(console.log).done();
}

我的查询是:

  1. 这是实现承诺链的正确方法吗?
  2. 在最后一个链中,我得到了空结果,但是当我将 o/p 打印到控制台时,我得到了正确的结果。
  3. 我对 getEducationDetail 函数进行迭代的方式是正确的方式,还是有其他选择。如果是这样,我怎样才能做到这一点。

【问题讨论】:

  • 您正在同步返回finalResult,但这些值仅以异步方式填充。

标签: node.js mongodb promise q


【解决方案1】:

你的代码可以更干净,请像这样调整一些部分

var updateUserDetails = function (req, res) {
  var finalResult = [];
  /* This is the function to update user using promise.*/
  return updateUserBasicDetails(req)
    .then(function (req) {
      _.each(req.body.userEducation, function (sd) {
        return getEducationDetail(req.body._id, sd)
          .then(function (result) {
            if (result) {
              return updateEducationDetails(sd, req.body._id)
            } 
            else {
              return addEducationDetails(sd, req.body._id)
            }
          })
          .then(function (result) {
            return result;
          })
          .then(function (result) {
            finalResult.push(result);
          })
        })

      return JSON.stringify(finalResult);
    })
    .then(function (finalResult) {
      res.json({
        "token": finalResult /*but here m getting empty result */
      })
    })
    .catch(function (e) { console.log(e); })
    .done();
}

如果你把_.each替换成q.all,上面的代码也可以更简洁。

  • 注意:您使用的 javascript es5 会使您的代码很长,您可以使用 javascript es6 缩短它并使用箭头函数而不是 function(x, y) {}

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 2015-04-15
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2015-08-15
    • 2015-07-08
    相关资源
    最近更新 更多