简体   繁体   English

使用 mongoose 从 mongodb 获取 object

[英]GET an object from mongodb using mongoose

The problem is probably simple, but my 2 AM brain can't understand what's going on anymore.问题可能很简单,但我凌晨 2 点的大脑无法理解发生了什么。 I'm trying to create a profile page that shows basic public info.我正在尝试创建一个显示基本公共信息的个人资料页面。 The way I'm trying to make it work is pulling out the users username from mongodb when registered the account by his specific _id.我试图让它工作的方式是在通过他的特定_id注册帐户时从 mongodb 中提取用户用户名。 If needed, verification I use is JWT.如果需要,我使用的验证是 JWT。

app.post('/api/user-profile', async (req,res) => {
  const { token } = req.body

  if(!token) {
      return res.json({ status: 'error', error: 'not logged in' })
  }

  try {
      const user = jwt.verify(token, JWT_SECRET)
      const userid = user.id
   
      const result = User.findOne({ userid })

      console.log(result)
      // return res.json({ status: 'ok', name: result })
  } catch(error) {
      // return res.json({ status: 'error', error: 'something went wrong' })
      console.log(error)
  }
})

I'm not sure what function should I use, findOne() or findById() .我不确定我应该使用什么 function, findOne()findById() I tried to look at the documentation at mongoose, but the explanation is a bit too hard for me to understand.我试图查看 mongoose 的文档,但解释对我来说有点难以理解。

PS User = the user registration model. PS用户=用户注册model。 If needed I can paste in the code.如果需要,我可以粘贴代码。

I don't have a lot of experience with mongoose but worked with Mongo quite a lot.我对 mongoose 没有太多经验,但与 Mongo 合作过很多。 findOne is a direct correspondent of Mongo's findOne function which receives a query in the format {"key": "expectedValue"}. findOne 是 Mongo 的 findOne function 的直接对应方,它接收格式为 {"key": "expectedValue"} 的查询。 If you want to use it to get data by id the query is {"_id": user.id}.如果您想使用它通过 id 获取数据,查询是 {"_id": user.id}。

Because fetching data by id is a common case, the lib added the method findByID which receives an ID, and then formats the query and makes an internal call to findOne.因为通过 id 获取数据是一种常见的情况,所以 lib 添加了 findByID 方法,它接收一个 ID,然后格式化查询并内部调用 findOne。

use findById instead of findOne if userid is _id and use await before the query, so do like this:如果userid_id则使用findById而不是findOne并在查询之前使用await ,所以这样做:

const result = await User.findById(userid)

if you want to use findOne:如果你想使用 findOne:

const result = await User.findOne({"_id" : userid})

if you want a plain object javascript use .toObject after query like this:如果你想要一个普通的 object javascript 在这样的查询后使用.toObject

const result = await User.findById(userid).toObject()
console.log(result)

For anyone interested, the answer is just like Mohammad Yaser Ahmadi said.对于任何有兴趣的人来说,答案就像 Mohammad Yaser Ahmadi 所说的那样。 Everything works fine, and by getting the username I did:一切正常,通过获取用户名我做了:

const user = jwt.verify(token, JWT_SECRET)
const userid = user.id

const result = await User.findById(userid)

const usersName = result.username

console.log(usersName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM