Reputation: 344
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
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
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