如何传递函数值 [英] how to pass function value
本文介绍了如何传递函数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了名为(user.js)的模型
I have created models called (user.js)
module.exports.show_deatils = function(req,res,callback){
var resultArray=[];
mongo.connect(url,function(err,db){
assert.equal(null,err);
var cursor=db.collection('users').find();
cursor.forEach(function(doc,err){
assert.equal(null,err);
resultArray.push(doc);
});
});
}
router.get('/restful', function(req, res){
User.show_deatils(function(req,res,resultArray){
req.session.resultArray=resultArray;
console.log(resultArray);
});
res.render('restful');
});
我创建了方法( show_details)$ c $在
user.js
模型中,我正在路由中调用该特定函数。每当加载页面(静态)时,我都希望显示数据 resultArray
。但是我被困在这里。
I have created a method("show_details")
in models user.js
and I am calling that particular function in routes. whenever the page (restful) gets loaded I want the data resultArray
to be displayed. But I am stuck here.
您能建议我如何解决该问题吗?
Can you please suggest me how to solve the issue?
推荐答案
您的用户模块show_deatils接受两个参数,您不传递任何参数
,而未使用回调函数,
you user module show_deatils takes two parameters, you are not passing any, and in callback you are not using,
user .js
user.js
module.exports.show_deatils = function(req,res,callback){
var resultArray=[];
mongo.connect(url,function(err,db){
assert.equal(null,err);
var cursor=db.collection('users').find();
cursor.forEach(function(doc,err){
assert.equal(null,err);
resultArray.push(doc);
});
callback(resultArray);
});
}
路线
route
router.get('/restful', function(req, res){
User.show_deatils(req,res,function(resultArray){
req.session.resultArray=resultArray;
console.log(resultArray);
res.render('restful');
});
});
这篇关于如何传递函数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文