javascript - bluebird的populateAsync怎么使用呢?

查看:70
本文介绍了javascript - bluebird的populateAsync怎么使用呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

这样写代码会报错:

 PostModel.findAsync({author:author}).populateAsync('author')
            .then(function (posts) {
                res.render('posts', {
                    posts: posts
                });
            })
            .catch(next);

错误为:

bluebird这个库的populate是怎么用的呢?

解决方案

  • PostModel.find is not a callback method or even asynchronous so PostModel.findAsync doesn't make sense. You shouldn't need to promisify your own classes you should just have your clasess to return promises to begin with.

  • If you have promisified mongoose somewhere:

Promise.promisifyAll(require("mongoose"));

You can do

PostModel.find({author: author })
    .populate("author")
    .execAsync()
    .then(function(posts) {
        res.render('posts', { posts: posts  });
    })
    .catch(next);

  • populate is not a function of bluebird, it's one of mongoose

  • Reference from here

这篇关于javascript - bluebird的populateAsync怎么使用呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆