2017-10-19 111 views
1

我正在練習我的node.js技能,我想提出一個用戶的個人資料頁面上的TOP10電影排行榜。用戶挑選的十部電影,現在我想在用戶文檔中,這些影片保存在我的數據庫。然而,這不正常的陣列仍然是空的(我能雖然改變「挑」屬性爲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") 
    }) 
}); 

這是用戶文檔的結構:

This is the structure of the user document:

回答