node.js - Mongoose find(), how to access the result documents? -


i started playing mongoose , mongo. have following code:

var ninjaschema = mongoose.schema({     name: string,     skill: number });  var ninja = mongoose.model('ninja',ninjaschema);  module.exports = { init : function(){     console.log('connecting database');     mongoose.connect('mongodb://localhost/mydb');     var db = mongoose.connection;     db.on('error', console.error.bind(console, 'connection error:'));     db.once('open', function callback () {       console.log('successfully connected!');     }); }, createninja : function(name,skill){     var n = new ninja({name:name,skill:skill});     n.save(function(err,n){         if (err)             console.log('saving failed');         console.log('saved '+ n.name);     }); }, getninjas : function(){     var res = null;     res = ninja.findone({},'name skill',function(err,docs){         if (err)             console.log('error occured in query');         return 'ninja name: '+docs.name+' ninja skill: '+docs.skill;     });      return res; } 

there no problem in adding entries database have problems retrieving them. bit confused how whole thing works. understanding following:

there schemas, classes in oop, blueprint record in database. model record, ok, maybe bit more, since saw can add method model. well... don't understand how use them. can give me clue they?

back subject: when issuing find command, calls anonymous function , docs should result right? how access them? since if log res following:

{ options: {}, safe: undefined, _conditions: {}, _updatearg: {}, _fields: { name: 1, skill: 1 }, _geocomparison: undefined, op: 'findone', model:  { [function: model]  base:    { connections: [object],     plugins: [],     models: [object],     modelschemas: [object],     options: {} },  modelname: 'ninja',  model: [function: model],  db:    { base: [object],     collections: [object],     models: {},     replica: false,     hosts: null,     host: 'localhost',     port: 27017,     user: undefined,     pass: undefined,     name: 'mydb',     options: [object],     _readystate: 1,     _closecalled: false,     _hasopened: true,     _listening: true,     _events: [object],     db: [object] },  schema:    { paths: [object],     subpaths: {},     virtuals: [object],     nested: {},     inherits: {},     callqueue: [],     _indexes: [],     methods: {},     statics: {},     tree: [object],     _requiredpaths: [],     options: [object],     _events: {} },  options: undefined,  collection:    { collection: [object],     opts: [object],     name: 'ninjas',     conn: [object],     queue: [],     buffer: false } } } 

also if use ninja.find(...,function(err,docs){ ... }) how go trough docs? or how retrieve records?

i've found fault. more of conceptual one: dealing asynchronous calls , trying return result function , don't know when execute. happens make request db query executed , return result, turns out null. code:

getninjas : function(){     var res = null;     ninja.find({},'name skill',function(err,docs){         if (err)             console.log('error occured in database');         console.log(docs);     });          return res; } 

returns null, but! console.log(docs) prints console values database, trying do. need make changes, pass callback executed upon receiving of results.

with changes code looks this:

getninjas : function(res){     var twisted = function(res){         return function(err, data){             if (err){                 console.log('error occured');                 return;             }             res.send('my ninjas are:\n');             console.log(data);         }     }      ninja.find({},'name skill',twisted(res)); } 

so able pass response object can send name of ninjas :)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -