-1

Hi I don't know why this function didn't do anything, even don't show anything on the console, and the callback function didn't work , Sorry if I made an obvious mistake I'm new with node js and NeDb.

here is my update function:

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;

and I called with:

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 database looks like:

{"_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":""} 

and the variables for the query

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

UPDATE 1

if I put all the code together ,not in separated module, works perfectly

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

maybe is something with been working in separated module

UPDATE 2 for simplicity

The models.js file

    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;

the runupdate.js file

    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);
        }
    });
efirvida
  • 4,592
  • 3
  • 42
  • 68

1 Answers1

1

There is a typo while you are calling update method.

{_id = game_id} change query with {_id : game_id}

Update :

Try to change your models.games.update instead of new function and directly assign db.games.update like following code block.

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

and you might call models.games.update(/*queries and cb functions*/).

İlker Korkut
  • 3,129
  • 3
  • 30
  • 51
  • thank, but didnt solve the problem, still not updating the records – efirvida Aug 18 '15 at 17:31
  • have you debugged your application? mark a breakpoint on db.games.update and your callbacks, we can sure your db.games.update method works. Because you have no error or any log. – İlker Korkut Aug 18 '15 at 17:39
  • I update the post, I forgot to say that the database configuration is on separated module maybe this is the reason – efirvida Aug 18 '15 at 18:12
  • and yes i debug my application and never get into any breakpoint in the callback function – efirvida Aug 18 '15 at 18:39
  • Was your method called literally when you debugged? Have you pointed update method also ? – İlker Korkut Aug 18 '15 at 19:31
  • when I call the debug in the first part of my question with breakpoint on the `db.games.update` the brek point stops there but even if i change the callback for a function didn't get into it, same as with breakpoints on `models.games.update` and its callback function. In the **UPDATE 1** part all works ok – efirvida Aug 18 '15 at 19:51
  • @efirvida I updated my answer according to your question's updates. – İlker Korkut Aug 19 '15 at 06:02
  • @efirvida you're welcome, also you might try to return `db.games.update` in a function but this solution is nicer than it in this situation. – İlker Korkut Aug 20 '15 at 15:57