I have this error TypeError: friendData.insert is not a function
PLS help…
tried changing the methods, the drop works.
I don’t know what to do anymore.
I don’t know of more write of this problem
const FriendSchema = new Schema({
user: {type: Schema.Types.ObjectId, ref: 'User'},
email: {type: String, unique: true, required: true},
friends: {type: Array},
subscribe: {type: Array},
requestFrineds: {type: Array},
})
module.exports = model('Friends', FriendSchema);
class friendService {
async addFriend(email, sender) {
const friendData = await FriendModel.findOne({email: email})
const senderData = await FriendModel.findOne({email: sender})
console.log(friendData, senderData)
await friendData.insert({requestFriends: sender});
await senderData.insert({subscribe: email});
friendData.save();
senderData.save();
// return friends;
}```
3
Answers
fixed
I think you mistake between
FriendModel
andFriends
The FriendSchema exports as Friends, so you should use like this:
and have you imported
Friends
schema yet? Let me know if it worksThere is no
insert
method. If you want to add them to array you can use thepush()
method like so:Note 1: you need to
await
thesave()
method too.There are better ways to achieve this. At the minute you are making 4 calls to the database. Two calls to get
friendData
andsenderData
then another two to update each document withsave()
. If you use findOneAndUpdate() you could half the number of database requests.Note 2: You also have a typo in your schema
requestFrineds
should berequestFriends
I presume?