首先感谢您帮我看这个问题,非常感谢
描述问题
整体采用Node.js的Express框架
我在数据库(MongoDB)中读取一个回答的评论对象(
answerComment
)出来,然后根据评论作者的ID读取了这个评论的作者信息(也是一个对象author
)通过
answerComment.author = author;
,进行赋值,然后通过console.log(answerComment.author);
显示了author这个对象的值;但是通过
res.status(200).json({comment: answerComment});
这句话向前端传递数据的时候,在前端通过var response = JSON.parse(request.responseText);
获得response之后,使用console.log(response.answerComment.author)
却是undefined
,输出console.log(response.answerComment)
的数据是有数据的,形式如下:
{
"__v":0,
"content":"de",
"author_id":"56e5944be3f709ad4c0395db",
"_id":"56efb9e40e28dd4427fd23a9",
"comment_count":0,
"deleted":false,
"dislikes":[],
"likes":[],
"update_at":"2016-03-21T09:07:48.105Z",
"create_at":"2016-03-21T09:07:48.105Z"
}
我的问题是为何在给对象赋值之后,在后端可以输出显示这个对象,但是在前端却显示不出来呢,是undefined呢?????
response.anserComment
这个对象不是没有 author
吗? 只有 author_id
~~
你的赋值语句可能是异步的,所以错觉的,其实他执行是在你返回以后,把resp .render放在author赋值异步回掉之中