【问题标题】:What am I doing wrong on my update function on nodejs/mongodb?我在 nodejs/mongodb 上的更新功能做错了什么?
【发布时间】:2013-09-29 00:02:23
【问题描述】:

您好,我目前是 nodejs 和 mongodb 的新手,我想做的是创建一个函数来更新我的用户模式中的赢、输、画记录。

我的架构:

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);

我的更新功能:

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)
    }
  });

问题是当我尝试更新时,它会更新当前数据,但会进入空白页面并抛出异常:

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)

但是我已经正确定义了win,这似乎是什么问题?

【问题讨论】:

    标签: javascript html node.js mongodb mongoose


    【解决方案1】:
    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
    

    在您的 sn-p v2 之后更新

    • 您的find 调用根本不匹配任何文档,因此user 为空
    • 仅供参考,您可以通过单个 findOneAndUpdate 操作同时进行查找和更新

    【讨论】:

    • 嗨,彼得!我已经更新了我的代码来完成你放置的所有 cmets。检查一下,我有一个新错误出现了
    • 请问怎么做可以吗? “你可以通过一次 findOneAndUpdate 操作同时进行查找和更新”我现在很迷茫
    • stackoverflow 指南鼓励“彻底研究”的问题。阅读猫鼬文档。与您花一分钟时间学习基本的 mongoose API 相比,对我来说,仅仅修复您的代码不会带来长期的好处。
    • 嗨,我做了你告诉我做的事情并研究了 findOneandUpdate 并感谢上帝它有效,我更新了我的代码 sn-p 的第 3 版,我有一个新问题说 win 没有定义?但我已经正确定义了胜利。这是为什么? @彼得里昂
    • 现在错误出现在您加载的下一页的模板中。完全不同的问题。
    【解决方案2】:
      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");
       }
     });
    

    希望您能理解您的问题并更新您的用户集合。

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多