该数组的行为不像数组

发布于 2025-01-10 06:41:28 字数 1576 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2025-01-17 06:41:28

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

console.log(stories[0].username) 

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

console.log(stories[0].username) 
合约呢 2025-01-17 06:41:28

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

Look like Stories is an array so you need to do stories[0].username.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文