[英]findOneAndUpdate fails using MEAN stack
Here's my code on my layout.handlebars 这是我在我的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");
});
And on my server here it is how i update my record but its not working is it because of the error? 在我的服务器上,这是我更新记录的方式,但是由于错误而无法正常工作吗?
//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
Now it says couldn't update my record Is it because im not using the _id instead? 现在它说无法更新我的记录,是因为我没有使用_id吗?
here's my error 这是我的错误
message: 'Cast to number failed for value "undefined" at path "e_money"', name: 'CastError', stringValue: '"undefined"', kind: 'number', value: undefined, path: 'e_money', reason: undefined } 消息:“在路径“ e_money”处,值“ undefined”的数字转换失败,名称:“ CastError”,字符串值:““ undefined””,种类:“数字”,值:undefined,路径:“ e_money”,原因:未定义}
By doing this achieved what i want 通过这样做达到了我想要的
User.findOneAndUpdate({"username":userName},
{"$set":{"e_money": newMoney }}, { upsert: true, returnOriginal:false },
function (err, doc) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.