【问题标题】:Return the value from a mongoose find从 mongoose 中返回值
【发布时间】:2016-05-19 02:25:23
【问题描述】:

当我检查链接以查看配置文件时,我正在尝试呈现模板(例如去 http://localhost:3000/user/testuser`),一切正常,但值不在页面上显示。这是我的代码:

Account.find({ username: req.params.username }, function(err, userWatch){
        res.render('account/profile',{
          title : userWatch.username,
          user : req.user,
          watchUser : userWatch,
        });

【问题讨论】:

  • console.log(userWatch) ?
  • [ { 关注:[],收藏:[],__v:0,emailAccount:'testuser@mysite.com',用户名:'testuser',_id:572b541f6693b24023a8dcac }]
  • 这是一个array,而您将其引用为object。使用 fineOne 获取数据
  • 使用 findOne 代替 find 得到单个结果

标签: node.js express mongoose


【解决方案1】:

您正在使用返回对象数组的Account.find()。如果您想要一个或没有对象,您应该使用Account.findOne() 来获取数据。

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2016-10-08
    • 2014-04-05
    • 2021-01-02
    相关资源
    最近更新 更多