【问题标题】:A function is calling itself, without any recursive calls implemented in it一个函数正在调用自己,没有在其中实现任何递归调用
【发布时间】:2011-10-02 06:35:28
【问题描述】:

我有这个功能可以从 mongodb 数据库中获取商店。

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)
            }
        }
    })
}

我在控制台中得到的输出看起来像

一个 b b

我希望它会是这样的

一个 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)
        })
}

【问题讨论】:

  • 你把一些没有上下文的代码扔给我们,并期望我们应该理解它并找到解决方案?
  • 你为什么不包含你调用这个函数的极其重要的代码?
  • 嗨,很抱歉没有早点添加被调用者。

标签: javascript mongodb node.js


【解决方案1】:

大概toArray()函数把它的输入转换成一个数组,然后把数组的每个元素传给它的参数中的闭包函数?

如果是这种情况,那么您看到的结果可能只是由toArray() 生成包含两个项目的数组引起的。这不需要任何东西以任何方式递归。

【讨论】:

  • 这与我在其他 2 个地方使用的语法相同,其中 toArray 函数按预期工作。
猜你喜欢
  • 1970-01-01
  • 2019-10-04
  • 2015-08-14
  • 2016-04-05
  • 1970-01-01
  • 2014-03-02
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多