简体   繁体   English

我在nodejs / mongodb上的更新功能在做什么?

[英]What am I doing wrong on my update function on nodejs/mongodb?

Hi I am currently new to nodejs and mongodb what I want to do is make a function to update my win,lose,draw record from my userschema. 嗨,我目前对nodejs和mongodb并不熟悉,我想做的是创建一个函数来更新我的用户图表上的胜利,失败,平局记录。

My Schema: 我的架构:

UserSchema = new mongoose.Schema({
    username:'string',
    password:'string',
    email:'string',
    //Change Made
    win:{ type: Number, default: 0 },
    lose:{ type: Number, default: 0 },
    draw:{ type: Number, default: 0 }
});

var db = mongoose.createConnection(app.get('MONGODB_CONN')),
    User = db.model('users', UserSchema);

My Function for updating: 我的更新功能:

app.post('/user/updateScores',function(req, res){
try{
      var query = req.body.username;
      User.findOneAndUpdate(query, { win : req.body.win, lose : req.body.lose, draw : req.body.draw }, function (err,user){
           if (err) res.json(err) ;
             req.session.loggedIn = true;
             res.redirect('/user/' + user.username);
      });
    }
    catch(e){
        console.log(e)
    }
  });

The problem is when I try updating, it updates the current data BUT goes to a blank page and throws an exception saying: 问题是当我尝试更新时,它会更新当前数据,但会转到空白页并引发异常:

ReferenceError: win is not defined
    at eval (eval at <anonymous> (C:\Users\ryan-utb\Desktop\RockScissorsPaper\node_modules\underscore\underscore.js:1176:16), <anonymous>:5:9)
    at template (C:\Users\ryan-utb\Desktop\RockScissorsPaper\node_modules\underscore\underscore.js:1184:21)
    at Function.exports.underscore.render (C:\Users\ryan-utb\Desktop\RockScissorsPaper\node_modules\consolidate\lib\consolidate.js:410:14)
    at C:\Users\ryan-utb\Desktop\RockScissorsPaper\node_modules\consolidate\lib\consolidate.js:106:23
    at C:\Users\ryan-utb\Desktop\RockScissorsPaper\node_modules\consolidate\lib\consolidate.js:90:5
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

but I already defined win properly, what seems to be the problem? 但是我已经正确定义了胜利,这似乎是什么问题?

User.update(
    {username:req.body.username},
    { win : req.body.win, lose : req.body.lose, draw : req.body.draw },
    function (err, data) { //look - no argument name "user" - just "data"
    //2 mistakes here that you should learn to never do
    //1. Don't ignore the `err` argument
    //2. Don't assume the query returns something. Check that data is not null.
    console.log(data);
    //The next line must be INSIDE this function in order to access "data"
    res.redirect('/user/' + data.username);
});     
//ACK don't try to access "data" (or "user") here. this is asynchronous code!
//this code executes EARLIER IN TIME than the User.update callback code

update after your snippet v2 摘录v2之后更新

  • your find call is simply not matching any documents, so user is null 您的find呼叫根本不匹配任何文档,因此user为null
  • FYI You can do a find and update at the same time with a single findOneAndUpdate operation 仅供参考,您可以通过一次findOneAndUpdate操作同时进行查找和更新
  User.update({
    username: req.body.username
  },{
    $set: {
        win : req.body.name,
        loose : req.body.loose
    }
  }, function(err, result) {
     if (err) {
       console.log(err);
   } else if (result === 0) {
      console.log("user is not updated");
   } else {
      console.log("user is updated");
   }
 });

I hope you can understand your issue and can update your User collection. 希望您能理解您的问题并可以更新您的用户集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM