为什么没有定义它,尽管我已经在顶部定义了newMoney,这是我的代码:
var deduct = 100;
var newMoney = {{user.e_money}} - deduct;
var username = getUserName(); //get the current user
// send a message to the server that the e-money value has changed
socket.emit('update e-money', username, newMoney);
console.log("Emitting the data to the server side - emoney: " + newMoney + " with the name money of : " + username);
//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);
});
在我的服务器端,这是我的代码:
//emoney
socket.on('update e-money', function (data) {
var userName = data.username;
var newMoney = data.newMoney;
//var query = {"name": userName};
// update the entry on the database
User.findOneAndUpdate({"username":userName}, {"$set":{"e_money": "300" }}, { upsert: true, returnOriginal:false }, function (err, doc) {
if (err) {
console.log("There was an error: " + err);
console.log(userName);
io.emit('update e-money error', { error: err });
} else {
io.emit('update e-money response', { newMoney: newMoney });
console.log(newMoney);
}
});
});
我不知道为什么它是未定义的.伙计们,你能帮帮我吗.任何人。
问题就在这里
var deduct = 100;
var newMoney = {{user.e_money}} - deduct;
var username = getUserName(); //get the current user
// send a message to the server that the e-money value has changed
socket.emit('update e-money', username, newMoney);
我应该这样做
var deduct = 100;
var newMoney = {{user.e_money}} - deduct;
var username = getUserName();
var compile = {newMoney, username};
然后在我的服务器端使用它:)
那我就这样发出来
socket.emit('update e-money', compile);