В настоящее время я практикую свои навыки node.js, и я делаю список фильмов top10 на странице профиля пользователя. Пользователь выбрал десять фильмов, и теперь я хочу сохранить эти фильмы в пользовательском документе в моей базе данных. Это, однако, не работает, массив все еще пуст (я смог изменить свойство "selected" на true, хотя). Может ли кто-нибудь сказать мне, почему? Вот код на стороне сервера:
router.get('/updateTop10', function (req, res, next) {
// the string that I want to add to the db array
var movieElement = req.query.movieElement;
// the specific index of the array (the position of the movie in the top10
var index = req.query.index;
// the user that is currently logged in
var user = req.user;
User.findOne({'username': user.username }, function (err, user) {
// I am able to switch picked to true
user.top10.picked = true;
// However my array is still empty after I run the code
user.top10.top10[index] = movieElement ;
user.save();
res.send("SUCCESS")
} )
});
Это структура пользовательского документа:
См. Https://github.com/Automattic/mongoose/issues/2654 Сделайте это
user.top10.top10.set(index, movieElement);