mongoose,express和node.js中回调函数的参数 [英] Arguments to callback function in mongoose, express and node.js

查看:229
本文介绍了mongoose,express和node.js中回调函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照MCV方法开发我的应用程序。我遇到一个问题,我不知道如何传递参数到回调函数。



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。模型('Animal',animalSchema)

animals.js(控制器)

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

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

这里有我的问题:列表在模型中只是执行回调而不传递任何参数?错误和动物是从哪里来的?



我想我可能错过了一些与回调相关的概念在节点

解决方案

函数列表需要





this.find()。exec(cb)也想要一个回调函数,所以我们传递从 list p>

execute 函数然后使用参数 err 和它收到的对象,在这种情况下 animals



return callback(err,objects)什么是最终调用回调函数。



有两个参数。这些是 err animals



回调函数作为参数传递,但从不会被调用,直到它被 exec 调用。此函数使用映射到 err animals


$ b的参数调用它$ b

编辑:



由于接缝不清楚,我会做一个简单的例子:

  var exec = function(callback){
//这里发生异步查询(在这种情况下不是这样,但是数据库会是)
var error = null;
var result =我是数据库中的大象;
return callback(error,result);
};

var link = function(callback){
//将回调传递给另一个函数
exec(callback);
};

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

在这里可以找到一个小提琴: http://jsfiddle.net/yJWmy/


I follow a MCV approach to develop my application. I encounter a problem that I dont know how the arguments are passed to the callback function.

animal.js (model)

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)

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. 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.

The execute function then calls the callback (executes it) with the parameters err and the object it received, in this case animals.

Inside of the list function happens something like return callback(err, objects) what is finally calling the callback function.

The callback function you passed now has got two parameters. These are err and animals

The key is : The callback function is passed as a parameter, but is never called until it is called by the exec. This function is calling it with parameters which are mapped to err and animals

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/

这篇关于mongoose,express和node.js中回调函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆