Node.js Mongoose find(),如何访问结果文档?
我刚开始玩猫鼬和猫鼬。我有以下代码:Node.js Mongoose find(),如何访问结果文档?,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我刚开始玩猫鼬和猫鼬。我有以下代码: var ninjaSchema = mongoose.Schema({ name: String, skill: Number }); var Ninja = mongoose.model('Ninja',ninjaSchema); module.exports = { init : function(){ console.log('Connecting to database'); mongoose.connect('m
var ninjaSchema = mongoose.Schema({
name: String,
skill: Number
});
var Ninja = mongoose.model('Ninja',ninjaSchema);
module.exports = {
init : function(){
console.log('Connecting to 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 the query');
return 'ninja name: '+docs.name+' ninja skill: '+docs.skill;
});
return res;
}
向数据库添加条目没有问题,但检索条目时遇到问题。我对整个事情的运作方式有点困惑。我的理解如下:
有一些模式,它们类似于oop中的类,所以只是数据库中记录的蓝图。这个模型是一个记录,好的,也许更多一些,因为我看到你可以在模型中添加一个方法。好。。。我真的不知道如何使用它们。你能告诉我它们到底是什么吗
回到主题:当发出find命令时,它调用匿名函数,结果应该是docs,对吗?现在我如何访问它们?从现在起,如果我记录res,我会得到以下结果:
{ 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 } } }
另外,如果我使用
Ninja.find(…,function(err,docs){…})
我如何浏览文档?或者如何检索我的记录 我找到了毛病。这更像是一个概念性的问题:我正在处理异步调用,试图从另一个函数返回结果,但不知道它何时执行。因此,我请求执行db查询并返回结果,结果为空。此代码:
getNinjas : function(){
var res = null;
Ninja.find({},'name skill',function(err,docs){
if (err)
console.log('error occured in the database');
console.log(docs);
});
return res;
}
返回null,但是!log(docs)将数据库中的所有值打印到控制台,这就是我要做的。现在我需要进行更改,最有可能的是传递一个回调,该回调将在收到结果时执行
更改后的代码如下所示:
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));
}
这样,我就可以传递响应对象,这样我就可以发送我的忍者的名字:)啊,你打败了我。我想给你一个很好的答案,但你得到了。您需要传递一个回调,并将结果发送给该回调。