javascript - findOne() doesn't work - is something wrong with my syntax? -
db creation:
var mongojs = require('mongojs'); var db = mongojs('rodrigo-contatos', ['rodrigo-contatos']);
i'm trying search in database code, using findone mongojs, code:
app.get('/detalhescontato/:id', function(req, res){ var id = req.params.id; console.log(id); db.contatos.findone({_id: mongojs.objectid(id)}, function(err, doc) { console.log(err); res.json(doc); });
console.log(id)
id correct findone not working no matter ;(.
"567a16ba28dee028f4a8ad78 <-- console log id
typeerror: cannot read property 'findone' of undefined @ /users/michel/documents/angularprojects/rodrigobranaslistatelefonica/server.js:48:12"
with mongojs, need explicitly identify collections want access properties of db
when call mongojs
create db
object. because you're trying access contatos
collection, name needs provided mongojs
call in second (array of strings) parameter:
var db = mongojs('rodrigo-contatos', ['contatos']);
or can skip short-cut access db
, collection later:
var contatos = db.collection('contatos'); contatos.findone(...);
Comments
Post a Comment