函数正在调用自身,而没有在其中实现任何递归调用
A function is calling itself, without any recursive calls implemented in it
我有这个函数从mongodb DB获取商店。
exports.find_shops = function(selector, fields, limit, skip, cb){
if(typeof fields == 'function'){
limit = 0
cb = fields
fields = {}
skip = 0
}
if(typeof limit == 'function'){
cb = limit
limit = 0
skip = 0
}
if(typeof skip == 'function'){
cb = skip
skip = 0
}
if(typeof selector == 'string'){
limit = 1
selector = {_id: new db.bson_serializer.ObjectID(selector)}
}
console.log('a')
Shop.find(selector, fields).limit(limit).toArray(function(err, shops){
console.log('b')
if(err){
throw new Error(err)
} else {
if(limit == 1){
cb(shops[0])
} else {
cb(shops)
}
}
})
}
我在控制台中得到的输出看起来像
bb
而我希望它是
b
怎么了?
编辑:exports.search = function(products, location, skip, cb){
if(typeof skip == 'function'){
cb = skip
skip = 0
}
this.find_shops({
products: {
$in: products
},
$or: [{
location: { $near: location , $maxDistance: 2 }
},
{
delivery: -1
},
{
delivery: {$lt: 2}
}]
}, {name: 1, location: 1, delivery: 1, products: 1}, 10, skip, function(shops){
shops.forEach(function(i,shop){
shops[i] = _.intersect(shop.products, products)
})
// now we have the products that user needs from this shop.
var combos = []
shops.forEach(function(i,shop1){
var combo = [i]
var p = shop1.products
shops.forEach(function(j,shop2){
if(i > j){
return
} else {
var newprod = _.intersect(p,shop2.products)
if(newprod.length == shop2.products.length){
return
} else {
p.push(shop2.products)
p = _.uniq
combo.push(j)
if(p.length == products.length){
combos.push(combo)
}
}
}
})
})
cb(combos)
})
}
假设toArray()
函数将其输入转换为数组,然后将数组的每个元素传递给其参数中的闭包函数?
如果是这种情况,那么您看到的结果可能仅仅是由toArray()
生成一个包含两个项目的数组引起的。不需要任何递归
相关文章:
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 使用jQuery动态添加表并在其中动态添加行
- Ajax调用在Firefox中不会自动响应
- AJAX调用在chrome扩展中未成功
- jQueryAjax调用在单独的.js文件中不起作用
- 使用Ajax调用在服务器上实时更新页面
- Javascript未在其中一个变量上输出0
- 函数从$(document).ready外部调用在$(document).ready中定义的函数
- Ajax调用在InternetExplorer中冻结UI,但在firefox中运行良好
- javascript在其中设置空格时会中断
- 调用在同一视图中使用javascript在视图中创建的函数
- .ajax调用在chrome中不起作用
- 我想在 javascript 中调用一个 php 函数,并在其中传递两个数组作为参数
- 同步函数调用涉及post-json调用,其中一个函数应该在另一个函数成功后成功
- 测试使用摩卡在其中调用承诺的函数
- 转义 $(“html”).ajaxError() 在其中一个 $.ajax() 调用中
- 两个并行调用,其中一个在javascript中成功
- 在其中调用async函数
- javascript调用的类似MVC动作,AntiForgery只在其中一个上工作,而不是另一个
- 函数正在调用自身,而没有在其中实现任何递归调用