本文介绍了节点js从mongodb obejct内部值返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从mongodb中检索到的商店对象,我对使用值store.comments感兴趣.

I have a store object i retrieved from mongodb,I am interested in using the value store.comments.

我记录了商店值,它是:

i logged the store value and it is:

store:{ _id: 57e246f73e63d635cce3d174,
  __v: 0,
  comments: 57e246f73e63d635cce3d177,
  loc: [ 105.832321, 105.233272 ],
  name: '24 store',
  reportedFalse: [],
  products: [],
  currentRanking: 
   { likes: 57e246f73e63d635cce3d175,
     dislikes: 57e246f73e63d635cce3d176 },
  timePosted: Wed Sep 21 2016 11:38:15 GMT+0300 (Jerusalem Summer Time) },

然后我记录了对象注释值的值- result.comments :

then i logged the value of the object comments value - result.comments:

store comments objectid: undefined

,当我稍后尝试通过注释值搜索时,似乎无法成功搜索它.因为搜索返回null,尽管我看到在数据库中我在"comments"表/模式中有一个具有相同ID的对应对象....

and it seems when i try to search later by the comments value that it doesn't succeed in searching with it. because the search returns null, although i see that in my db i have a corresponding object in "comments" table/schema with the same id....

Comments.findById(commentsId,function(commentsErr,commentsArrObj){
...
cb(null, commentsArrObj._id);
                    }

我得到:

TypeError: Cannot read property '_id' of null

这是我的架构的一部分以供参考:

heres part of my schema for reference:

        name: String,
        comments: mongoose.Schema.Types.ObjectId, // each comment has a object id - responder, link curl? crud?  to his profile, profile pic, date time.
        timePosted : { type: Date, default: Date.now },
        currentRanking: {
            likes: mongoose.Schema.Types.ObjectId, // a votes document
            dislikes: mongoose.Schema.Types.ObjectId // a votes document
        },

推荐答案

在您的模式中,您必须使用'ref'

In your schema you have to use 'ref'

comments: mongoose.Schema.Types.ObjectId,ref:'comments'(modal name of comments)

引用注释架构并使用填充注释来获取注释数据.

to refer comments schema and use populate the comments to get comments data.

这篇关于节点js从mongodb obejct内部值返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 07:53