该数组的行为不像数组

The array doesn't behave like an array

我正在使用 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

有人可以向我解释一下,这是怎么回事,我如何才能访问用户名?

看起来 Stories 是一个数组,因此您需要执行 stories[0].username

由于 stories 是一个数组,您可以使用索引访问数组中的元素。在这种情况下,如果你想记录第一个故事的用户名,你需要做

console.log(stories[0].username)