【发布时间】:2017-11-22 11:45:13
【问题描述】:
这是我的 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”中值“未定义”的数字转换失败', 名称:'CastError', stringValue: '"未定义"', 种类:'数字', 值:未定义, 路径:'e_money', 原因:未定义}
【问题讨论】:
-
console.log(newMoney | err);打印什么? -
它实际上并没有打印出来。我不知道为什么即使它有一个console.log
-
您是否尝试调试并查看失败的地方?尝试将
console.log上移到io.emit之前 -
我会编辑我的问题
-
尝试把findUpdate里面的
query改成{"username": userName}
标签: javascript node.js mongoose mean-stack