javascript - A function is calling itself, without any recursive calls implemented in it -



javascript - A function is calling itself, without any recursive calls implemented in it -

i have function shops 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) } } }) }

the output in console looks like

a b b

whereas expect be

a b

whats wrong here?

edit:

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) }) // have products user needs shop. var combos = [] shops.foreach(function(i,shop1){ var combo = [i] var p = shop1.products shops.foreach(function(j,shop2){ if(i > j){ homecoming } else { var newprod = _.intersect(p,shop2.products) if(newprod.length == shop2.products.length){ homecoming } else { p.push(shop2.products) p = _.uniq combo.push(j) if(p.length == products.length){ combos.push(combo) } } } }) }) cb(combos) }) }

presumably toarray() function converts input array, , passes each element of array closure function in parameter?

if that's case, result you're seeing caused toarray() generating array of 2 items. doesn't needs recursive in way.

javascript mongodb node.js

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -