嗨我不知道为什么这个函数没有做任何事情,甚至没有在控制台上显示任何内容,并且回调函数没有工作,对不起,如果我犯了一个明显的错误,我是 node 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
模型.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);
}
});