为了账号安全,请及时绑定邮箱和手机立即绑定

mongoose联合查询populate如何使用?

mongoose联合查询populate如何使用?

HUH函数 2018-11-21 17:13:36
使用node练习博客项目的时候,文章需要关联用户和分类,评论集合,所以就用到了mongoose的populate查询,可是无法返回想要的数据,代码如下:保存文章的:    // 拿到session中的用户信息    let authorObj = {author: ObjectId(req.session.userInfo.userID)};    let cateObj = {category: ObjectId(req.body.category)};    // 增加到数据中    postData = Object.assign(req.body,authorObj,cateObj);    // 添加新数据    postModel.create(postData)        .then((result)=>{            if(!result){                reMessage.msg='文章保存错误';                reMessage.code=2003;                res.send(reMessage);                return;            }            reMessage.msg='文章发表成功';            reMessage.code=0;            res.send(reMessage);        });返回数据的:具体代码就如上,为什么不行呢?
查看完整描述

1 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

是创建Schema的字段ref引用错误,应该引用userModel = db.model('user',suerSchema)的第一个参数,而不是userModel

查看完整回答
反对 回复 2018-12-26
  • 1 回答
  • 0 关注
  • 845 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号