Home > Mobile >  why am i getting error "insertOne is not a function" in mongodb?
why am i getting error "insertOne is not a function" in mongodb?

Time:08-06

the code connects to the mongodb database:

const dbConnect = async () => {
    try {
      await client.connect();
  
      const db = client.db('smile-tracker');
      const users = db.collection('users');
      return users;

    } catch (e) {
      console.log('Database connect error!');
      console.log(e);
    }
}

const users = dbConnect();
await users.insertOne({user: username});

but it gives me an error: users.insertOne is not a function

what to do ?

upd: it used to work

CodePudding user response:

You are calling Promise.insertOne of course it's not a function.

You need to await it

const users = await dbConnect();
  • Related