节点 JS 在嵌套函数中返回
Node JS return within nested function
我正在尝试从这个嵌套函数中返回 itemInfo[0] 的值。谁能帮助我应该如何通过回调返回此值?
function findItem(item) {
var itemInfo = [];
Item.findItem(item, function(err, itemInfo){
itemInfo[0].info = _.unescape(itemInfo[0].info);
itemInfo[0].title = _.unescape(itemInfo[0].title);
// console.log(itemInfo[0]);
return itemInfo[0];
});
};
使用 cb 参数后将其设置为 null,并在调用之前检查其有效性。
函数 findItem(item, cb) { var itemInfo = []; Item.findItem(item, function(err, itemInfo){ 如果 (cb) { itemInfo[0].info = _.unescape(itemInfo[0].info); itemInfo[0].title = _.unescape(itemInfo[0].title); console.log(itemInfo[0]); cb( itemInfo[0] ); cb = 空; } });};
如果返回返回值怎么办?
function findItem(item) {
var itemInfo = [];
return Item.findItem(item, function(err, itemInfo){
itemInfo[0].info = _.unescape(itemInfo[0].info);
itemInfo[0].title = _.unescape(itemInfo[0].title);
// console.log(itemInfo[0]);
return itemInfo[0];
});
};
相关文章:
- 使用返回函数sinde.attr()jquery元素
- 从自执行函数返回函数的Javascript性能命中率
- Node Express Handlebars帮助程序未返回函数的结果
- 未在Firefox中执行PageMethod的返回函数
- 对返回函数的函数感到困惑
- 从承诺返回不返回函数会导致警告
- 从函数返回函数而不调用返回的函数
- Javascript,闭包中的返回函数如何与外部函数连接
- 为什么Coderbyte.com's的Javascript模板喜欢返回函数的原始参数
- 对象函数返回函数而不是值
- 从外部函数(数组)了解返回函数(x)
- 从Javascript类对象返回函数
- 调用Typescript setter don't返回函数,尽管关联的getter可以工作
- 使用依赖注入在 JavaScript 中返回函数
- 通过单击JSP和javascript加载两个返回函数
- 需要说明:无法理解返回函数的javascript
- 简单的onClick返回函数不起作用
- 为什么这个闭包返回函数
- JS函数返回函数供以后使用-未定义参数
- Coffeescription类中的方法返回函数而不是字符串