let friends = [{ Friend: 'Example', id: '123' },
{ Friend: 'Example', id: '123' },
{ Friend: 'Example', id: '123' }]
let name = John;
const userSchema = new mongoose.Schema({
name: String,
friends: [{ Friend: String, id: String}],
date: { type: Date, default: Date.now }
})
How can I insert the name at 'name' and the array at 'friends' data? Can I create an empty database entry and then push it in or should I create a new entry with both values?
Try this way...
const addToFriends = async () => {
try {
const user = await User.findOne({ name: user name });
user.name = name;
user.friends.friend = name;
await user.save();
} catch(err) {
console.log(err);
}
};