router.post('/add/post/:id', async(req, res) => {
try {
const {text, img} = await req.body;
const user = await User.findById(req.params.id);
await User.updateOne({
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
password: user.password,
background: user.background,
photo: user.photo,
followers: user.followers,
posts: []
},
{$push: {'posts': {$each: [text, img]}}});
res.redirect('/profile/'+user._id);
} catch (e) {
console.log('error ' + e);
}
})
posts
только елементы массива, а не объекты, также они добавляются только один раз, а нужно хоть сколько. router.post('/add/post/:id', async(req, res) => {
try {
const {text, img} = await req.body;
const user = await User.findById(req.params.id);
await User.updateOne({
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
password: user.password,
background: user.background,
photo: user.photo,
followers: user.followers
},
{$push: {posts: {$each: [{text, img}]}}});
res.redirect('/profile/'+user._id);
} catch (e) {
console.log('error ' + e);
}
})