簡體   English   中英

findOneAndUpdate使用MEAN堆棧失敗

[英]findOneAndUpdate fails using MEAN stack

這是我在我的layout.handlebars上的代碼

 $(document).ready(function(){
        var socket = io();

        //     //understand button
        $(".understandbtn").click(function(){
            //reset the timer every 3 second of interval
            $('.actionBtnFloat').css('z-index','0');

            //e_money
             var deduct = 100;
             var newMoney = {{user.e_money}} - deduct;

  // send a message to the server that the e-money value has changed
 //get the current user
             socket.emit('update e-money',getUserName(), newMoney);

             // console.log("Emitting the data to the server side - emoney" + getUserName() + "with the name money of :" + newMoney);
            //end 

            clearTimeout(interval);
            //send the data to the server
            socket.emit('chat message', getUser());
            var interval = setTimeout(function(){
                $('.'+getUser()).fadeIn();
            },5000);
        });

             socket.on('update e-money response', function (data) {
             alert("Your money is: "+ data.newMoney);
             console.log("Your money is:" + data.newMoney);
             });

            socket.on('update e-money error', function (err,data) {
                if(err) throw err;
            // alert("Could not update your money: "+ data.error);
            // console.log("Could not update your money"+ data.error);
                alert("Sucessfully updated  your money");
                console.log("Sucessfully updated your money");
            });

在我的服務器上,這是我更新記錄的方式,但是由於錯誤而無法正常工作嗎?

//emoney
socket.on('update e-money', function (data) {
var userName = data.username;
var newMoney = data.newMoney;
var query = {username: userName};

// update the entry on the database
User.findOneAndUpdate(query, { e_money: newMoney }, { upsert: true, new: true }, function (err, doc) {
if (err) {
  io.emit('update e-money error', { error: err });
  console.log(err);
} else {
    io.emit('update e-money response', { e_money: newMoney });
    console.log(newMoney);
}
});
});

//end emoney

現在它說無法更新我的記錄,是因為我沒有使用_id嗎?

這是我的錯誤

消息:“在路徑“ e_money”處,值“ undefined”的數字轉換失敗,名稱:“ CastError”,字符串值:““ undefined””,種類:“數字”,值:undefined,路徑:“ e_money”,原因:未定義}

通過這樣做達到了我想要的

User.findOneAndUpdate({"username":userName}, 
{"$set":{"e_money": newMoney }}, { upsert: true, returnOriginal:false }, 
function (err, doc) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM