2017-06-20 38 views
0

是我的代碼:未捕獲的ReferenceError:爲什麼不能,雖然我已經在這裏頂部定義newMoney定義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); 
} 
    }); 
    }); 

我知道爲什麼它是未定義的。你能幫助我嗎?任何人。

+0

所以你說先生我應該這樣做t他的'{newMoney:data,newMoney}' – TheGinxx009

+0

ahm我很清楚我確信我會推動即時通訊如果沒有錯誤,這個人會執行 – TheGinxx009

回答

0

的問題是在這裏

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); 
相關問題