Here's my code on my 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?
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 }
By doing this achieved what i want
User.findOneAndUpdate({"username":userName},
{"$set":{"e_money": newMoney }}, { upsert: true, returnOriginal:false },
function (err, doc) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.