0

i have to save a json in array but i can't in

controllers/api.js

exports.install = function() {

    F.cors('/api/db/*',['post'],false)

    F.route('/api/db/',saveNoSql,['post']);
};


function saveNoSql(){
    console.log('dentro saveNoSql');
    var self = this;
    var body = self.req.body;

    var users = NOSQL('db');
    users.find().make(function(builder){
        builder.and()
        builder.where('name','Report');
        builder.where('entries['+0+'].name','Report');
        builder.callback(function(err,model){

        })
    })
}

databases/db.nosql

{"name":"Report","user":"rep","password":"admin","odata":"false","entries":[{"name":"Report","appointment":[{"idOrder":"1","order":"order 1","supplier":"fornitore 1","hours":"numero ore","actions":"icone azioni"}]},{"name":"Admin","appointment":[]}]}
{"name":"Admin","user":"adm","password":"admin","odata":"true","entries":[]}
{"name":"Mike","user":"mike","password":"admin","odata":"true","entries":[]}

Here you see that i have to save a req.body in name->Report/entries->Report/appointment and i guess to do that with find() and insert() , right?

1 Answers1

0

This is solution, but not much effective:

var users = NOSQL('db');

users.find().filter(doc => doc.name === 'Report' && doc.entries && doc.entries[0] && doc.entries[0].name === 'Report').callback(function(err, model) {
    console.log(err, model);
});

I recommend to update all documents by adding new filter fields for much simpler filtering. Documentation: https://docs.totaljs.com/latest/en.html#api~DatabaseBuilder~builder.filter

Peter Sirka
  • 748
  • 5
  • 10
  • ok , but i still don't read a doc.entries because it returns me an undefined object – Michele Spinello Nov 29 '17 at 09:44
  • Please install latest version of Total.js framework. I have tested my code with your data and everthing works correctly. Please write me Total.js Messenger, it will be faster. – Peter Sirka Nov 29 '17 at 10:24