NeDB 没有更新记录

NeDB didn't update the records

嗨,我不知道为什么这个函数没有做任何事情,甚至不在控制台上显示任何东西,回调函数也不起作用,如果我犯了一个明显的错误,我很抱歉我是新手使用节点 js 和 NeDb。

这是我的更新函数:

var Datastore = require('nedb'),
    db = {
        games: new Datastore({filename: './backend/data/games.db', autoload: true})
    };

var models = { 
     games : {
              update: function (query, update, options, callback) {
                           db.games.update(query, update, options,callback)
             }
            }
           }
module.exports = models;

我打电话给:

var models = require('./models');

models.games.update({_id = game_id}, {$set: {fen: game.fen(), pgn: game.pgn()}}, {}, function(err,numDocs,docs){
      if (err){
         console.log(err);
      } else {
         console.log(numDocs);
         console.log(docs);
      }
    });

mi 数据库看起来像:

{"_id":"Egw17uRnAd5sdaKXVlOfxRQ6zr4VnSvFghiXkXHyCi9oiDMqDS","startpos":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","pgn":"","w_id":"","b_id":""}
{"_id":"6ZlTTQgk2hEhorTGeTV45kkRsUvrxfmROiCJMqLRQoTnvabhqK","startpos":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","pgn":"","w_id":"","b_id":""} 

和查询的变量

game_id = '6ZlTTQgk2hEhorTGeTV45kkRsUvrxfmROiCJMqLRQoTnvabhqK'
game.pgn() = '1. e4 e5 2. Nf3'
game.fen() = 'rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2'

更新 1

如果我将所有代码放在一起,而不是在单独的模块中,就可以完美地工作

var Datastore = require('nedb'),
    db = {
          games: new Datastore({filename: './backend/data/games.db', autoload: true})
          };
db.games.update({_id : game_id}, {$set: {fen: game.fen(), pgn: game.pgn()}}, {}, function(err,numDocs,docs){
   if (err){
       console.log(err);
   } else {
       console.log(numDocs);
       console.log(docs);
   }
});

也许是在单独的模块中工作的东西

更新 2 为简单起见

models.js 文件

    var Datastore = require('nedb'),
        db = {
            games: new Datastore({filename: 'database.db', autoload: true})
        };


    var models = {
        games: {
            create: function (id, startpos, fen, pgn, w_id, b_id, callback) {
                db.games.insert({_id: id, startpos: startpos, fen: fen, pgn: pgn, w_id: w_id, b_id: b_id}, callback);
            },
            update: function (query, update, options, callback) {
                db.games.update(query, update, options, callback)
            }
        }
    };

    module.exports = models;

runupdate.js 文件

    var models = require('./models');

    models.games.create('6ZlTTQgk2hEhorTGeTV45kkRsUvrxfmROiCJMqLRQoTnvabhqK', 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', '', '', '');

    models.games.update({_id: '6ZlTTQgk2hEhorTGeTV45kkRsUvrxfmROiCJMqLRQoTnvabhqK'}, {$set: {fen: 'r1bqk1nr/pppp1ppp/2n5/2b1p3/2B1P3/5N2/PPPP1PPP/RNBQ1RK1 b kq - 5 4', pgn: '1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 4. O-O'}}, {}, function(err,numDocs,docs){
        if (err){
            console.log(err);
        } else {
            console.log(numDocs);
        }
    });

调用更新方法时出现错字。

{_id = game_id} 将查询更改为 {_id : game_id}

更新:

尝试更改您的 models.games.update 而不是新函数,并像下面的代码块一样直接分配 db.games.update

var models = {
  games : {
     update : db.games.update
  }
}

你可能会打电话给 models.games.update(/*queries and cb functions*/)