嗨,我不知道爲什麼這個功能沒有做任何事情,即使沒有在控制檯上顯示任何東西,並且回調函數不起作用,對不起,如果我做了一個明顯的錯誤我是新的節點js和NeDb。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);
}
});
英里數據庫的樣子:
{"_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);
}
});
您是否嘗試過直接調用'db.games.update(查詢,更新,選項,回調)'而不是'models.games.update'? –
是的,結果是德samme,沒有更新 – efirvida