【问题标题】:promisified mongoose/mongodb save not returning success to ajax call?承诺 mongoose/mongodb 保存不返回成功到 ajax 调用?
【发布时间】:2014-09-14 00:07:45
【问题描述】:

我有一个great promisified findOneAsync thanks to @BenjaminGruenbaum,但由于某种原因,在保存运行后,ajax 没有运行success 函数..这仅发生在 promisified 代码中。

这是应该运行成功函数 refreshStories 的 ajax:

console.log('there is a story: ' + AjaxPostData.story);
// make an ajax call
$.ajax({
dataType: 'json',
data: AjaxPostData,
type: 'post',
  url: liveURL + "/api/v1/stories",
  success: refreshStories,
  error: foundError
});

这是具有承诺的 API 调用:

router.route('/stories')

// create a story (accessed at POST http://localhost:4200/api/v1/stories)
.post(function(req, res) {

  var story = new Models.Story();
  var toArray = req.body.to; // [ 'user1', 'user2', 'user3' ]
  var to = Promise.map(toArray,function(element){
      return Promise.props({ // resolves all properties
          user : Models.User.findOneAsync({username: element}), 
          username : element, // push the toArray element
          view : {
              inbox: true,
              outbox: element == res.locals.user.username,
              archive: false
          },
          updated : req.body.nowDatetime
      });
  });
  var from = Promise.map(toArray,function(element){ // can be a normal map
      return Promise.props({
          user : res.locals._id,
          username : res.locals.username,
          view : {
              inbox: element == res.locals.user.username,
              outbox: true,
              archive: archive,
          },
          updated : req.body.nowDatetime
      });
  });
  Promise.join(to, from, function(to, from){
      story.to = to;
      story.from = from;
      story.title = req.body.title;
      return story.save();
  }).then(function(){
      console.log("Success! Story saved!");
  }).catch(Promise.OperationalError, function(e){
      console.error("unable to save findOne, because: ", e.message);
      console.log(e);
      res.send(e);
      throw err;
      // handle error in Mongoose save findOne etc, res.send(...)
  }).catch(function(err){
      console.log(err);
      res.send(err);
      throw err; // this optionally marks the chain as yet to be handled
      // this is most likely a 500 error, while the top OperationError is probably a 4XX
  });

});

【问题讨论】:

    标签: ajax node.js mongodb promise bluebird


    【解决方案1】:

    在加入回调中,您返回story.save()。这不是承诺。

    您可能想要做的是:

    var saveFunc = Promise.promisify(story.save, story);
    return saveFunc();
    

    这将保证 save() 方法。你也可以Promise.promisifyAll(story)return story.saveAsync()

    所以你的代码会变成:

    Promise.join(to, from, function(to, from){
      story.to = to;
      story.from = from;
      story.title = req.body.title;
      var saveFunc = Promise.promisify(story.save, story);
      return saveFunc();
    }).then(function(saved) {
      console.log("Sending response.");
      res.json(saved);
    }).catch ...
    

    【讨论】:

    • 谢谢!我应该把var saveFun = .. 放在哪里?
    • 你可以用这两行替换story.save()
    • hmm..试过了..似乎不起作用,它可以保存但不会成功..有什么建议吗?
    • 那个方法被调用了吗?即连接中的代码?
    • 刚刚查看了数据库,故事正在正确保存..是你指的方法吗?
    猜你喜欢
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2015-11-12
    • 2016-06-15
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多