Mongoose findbyid() return null

前端 未结 5 827
时光取名叫无心
时光取名叫无心 2021-01-13 01:04

I am trying to find a record in my mongo db by its id

No matter I use findbyid(), findone(id,...), it return null

here is my code. what is the solution?

5条回答
  •  隐瞒了意图╮
    2021-01-13 01:21

    Convert the string into ObjectId type before querying

    var ObjectID = require('mongodb').ObjectID;   
    async function updateData(id){
      const result = await Data.findById(new ObjectID(id));
      console.log(result);
    }
    

    refer: this answer.

提交回复
热议问题