Fateme Ahmadi
Fateme Ahmadi

Reputation: 344

insert is not a function in mongo

i write this simple code to insert something in databse. my database is mongodb and I want to insert an object( a document ) in a collection in nodejs. something like this :

await InboxMessage.insert({
userId,
title,
body,
viewed: false,
deeplink: customData && customData.deeplink ? customData.deeplink : null,

})

but i get an error that says to me : error in reply to comment: TypeError: InboxMessage.insert is not a function

Where did I go wrong?

Upvotes: 1

Views: 4994

Answers (2)

Nikhil Sood
Nikhil Sood

Reputation: 1

It will work with insertOne() function. Try using that...I was getting the same error which resolved by using insertOne() function and data was inserted in collection.

Upvotes: 0

Saurabh Mistry
Saurabh Mistry

Reputation: 13689

try using insertOne function :

await db.collectionName.insertOne({});

await InboxMessage.insertOne({
 userId,
 title,
 body,
 viewed: false,
 deeplink: customData && customData.deeplink ? customData.deeplink : null,
});

if you are using mongoose npm then use create or save

await ModelName.create({});

await InboxMessage.create({
     userId,
     title,
     body,
     viewed: false,
     deeplink: customData && customData.deeplink ? customData.deeplink : null,
    });

Upvotes: 3

Related Questions