该数组的行为不像数组
我正在使用express、node 和mongoose。如果我访问我的 mongoDB 数据库并 console.log 它,我会得到我的数组:
module.exports ={
stories: function(req, res, next){
Story.find(function(err, stories){
if(err) return handleError(err);
console.log(stories)
res.render('dashboard', {
err,
stories
})
})
}
}
输出
'0': {
_id: new ObjectId("6206613aeb4f95fd983f94ba"),
username: 'David N.',
content: ' content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content,v',
date: 2022-02-11T13:14:34.461Z,
__v: 0
}
但我无法执行点表示法!
module.exports ={
stories: function(req, res, next){
Story.find(function(err, stories){
if(err) return handleError(err);
console.log(stories.username)
res.render('dashboard', {
err,
stories
})
})
}
}
那么输出是: undefined
请有人向我解释一下,发生了什么事以及我如何才能访问用户名?
I'm working with express, node and mongoose. If I access my mongoDB database and console.log it, I get my array:
module.exports ={
stories: function(req, res, next){
Story.find(function(err, stories){
if(err) return handleError(err);
console.log(stories)
res.render('dashboard', {
err,
stories
})
})
}
}
The output
'0': {
_id: new ObjectId("6206613aeb4f95fd983f94ba"),
username: 'David N.',
content: ' content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content,v',
date: 2022-02-11T13:14:34.461Z,
__v: 0
}
BUT I cannot perform dot notation!
module.exports ={
stories: function(req, res, next){
Story.find(function(err, stories){
if(err) return handleError(err);
console.log(stories.username)
res.render('dashboard', {
err,
stories
})
})
}
}
Then the output is: undefined
Can please someone explain to me, what is going on and how I can just access the username for example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于故事是一个数组,因此您可以使用索引访问数组内的元素。在这种情况下,如果你想记录第一个故事的用户名,你需要这样做
Since stories is an array, you can access the elements inside the array using indexing. In this case if you want to log the username of 1st story, you need to do
看起来 Stories 是一个数组,因此您需要执行
stories[0].username
操作。Look like Stories is an array so you need to do
stories[0].username
.