简体   繁体   English

mongoose,express和node.js中回调函数的参数

[英]Arguments to callback function in mongoose, express and node.js

I follow a MCV approach to develop my application. 我遵循MCV方法来开发我的应用程序。 I encounter a problem that I dont know how the arguments are passed to the callback function. 我遇到一个问题,我不知道如何将参数传递给回调函数。

animal.js (model) animal.js(型号)

var mongoose = require('mongoose')
, Schema = mongoose.Schema

var animalSchema = new Schema({ name: String, type: String });
animalSchema.statics = {
     list: function(cb) {
         this.find().exec(cb)
     }
}
mongoose.model('Animal', animalSchema)

animals.js (controller) animals.js(控制器)

var mongoose = require('mongoose')
, Animal = mongoose.model('Animal')

exports.index = function(req, res) {
    Animal.list(function(err, animals) {
        if (err) return res.render('500')
        console.log(animals)
    }
}

Here comes my question: Why can the "list" in the model just execute callback without passing any argument? 我的问题出现了:为什么模型中的“列表”只是执行回调而不传递任何参数? Where do the err and animals come from actually? 错误和动物从哪里来?

I think I may miss some concepts related to callback in node.js and mongoose. 我想我可能会错过与node.js和mongoose中的回调相关的一些概念。 Thanks a lot if you can provide some explanation or point me to some materials. 非常感谢您提供一些解释或指出我的一些材料。

The function list wants to have a callback function passed. 函数列表希望传递回调函数。

So you pass a callback function. 所以你传递了一个回调函数。

this.find().exec(cb) wants a callback function too, so we pass the callback we got from the list function. this.find().exec(cb)想要一个回调函数,所以我们传递了我们从list函数得到的回调。

The execute function then calls the callback (executes it) with the parameters err and the object it received, in this case animals . 然后, execute函数使用参数err和它接收的对象(在本例中为animals )调用回调(执行它)。

Inside of the list function happens something like return callback(err, objects) what is finally calling the callback function. 在列表函数内部发生类似return callback(err, objects)内容,最终调用回调函数。

The callback function you passed now has got two parameters. 您现在传递的回调函数有两个参数。 These are err and animals 这些是erranimals

The key is : The callback function is passed as a parameter, but is never called until it is called by the exec . 关键是:回调函数作为参数传递,但在exec调用之前永远不会调用。 This function is calling it with parameters which are mapped to err and animals 此函数使用映射到erranimals参数调用它

EDIT: 编辑:

As it seams unclear i will do a short example: 由于接缝不清楚我会做一个简短的例子:

var exec = function (callback) {
    // Here happens a asynchronous query (not in this case, but a database would be)
    var error = null;
    var result = "I am an elephant from the database";
    return callback(error, result);
};

var link = function (callback) {
    // Passing the callback to another function 
    exec(callback);
};

link(function (err, animals) {
    if (!err) {
        alert(animals);
    }
});

Can be found as a fiddle here : http://jsfiddle.net/yJWmy/ 可以在这里找到一个小提琴: http//jsfiddle.net/yJWmy/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM