2016-04-14 43 views
0

我想要使用mongo數據庫查詢&更新對象'associativeArray'的數組想要將它返回到下一個然後塊所以。 但是,當我運行應用程序時,我在數組中得到舊值。不能夠從nodejs異步mongo數據庫查詢返回對象數組

var express = require('express'); 
var router = express.Router(); 
var mongoose = require('mongoose'); 
var bodyParser = require('body-parser'); 

var error_imports = require('../models/models.js').errorimport; 
var users = require('../models/models.js').users; 
var magento_categories = require('../models/models.js').magentocategories; 
var MongoClient = require('mongodb'); 

var allprodsArray = error_imports.find({sku_number:"889059223895-AllSaints"}); 
var allCategoriesArray = magento_categories.find(); 
//var allproducts = []; 
var allCategories = []; 
var mainCategories = []; 
var pro = []; 
var name = "Sagar"; 
var collectionOne = []; 

router.get('/', function (req, res) { 
    var allprodsArray = error_imports.find().limit(10); 
    var allproducts = []; 
    var products = []; 

    allprodsArray.exec(function(err,prods){ 
     if(err) 
      return cosole.log(err); 
     prods.forEach(function(prod){ 
      var elem = new Object(); 
      elem["id"] = prod._id 
      elem["tags"] = prod.id; 

      allproducts.push(elem); 
      pro = allproducts; 

     }); 
    res.render('index.html',{products:pro}); 
    }); 
}); 

router.post('/', function (req, res) { 
    var allprodsArray_1 = error_imports.find().limit(10); 
    var suffix_1, 
     suffix_2, 
     suffix_3, 
     suffix_4; 

    var associativeArray = new Object(); 
    var ass = []; 
    var conn = mongoose.connection; 
    var mag_cat_query; 

    allprodsArray_1.exec(function(err,prods1){ 
     if(err) 
      return cosole.log(err); 
     prods1.forEach(function(prod1){ 
      var selectedCategory = ''; 
      var id = prod1._id; 
      suffix_1 = 'statessource_' + id; 
      suffix_2 = 'citiessource_' + id; 
      suffix_3 = 'city_' + id; 
      suffix_4 = 'area_' + id; 

      if(req.body[suffix_1]!=null){ 
       selectedCategory = selectedCategory +'/'+ req.body[suffix_1]; 
       if(req.body[suffix_2]!=null){ 
        selectedCategory = selectedCategory +'/'+ req.body[suffix_2]; 
        if(req.body[suffix_3]!=null){ 
         selectedCategory = selectedCategory +'/'+req.body[suffix_3]; 
         if(req.body[suffix_4]!=null){ 
          selectedCategory = selectedCategory +'/'+req.body[suffix_4]; 
         } 
        } 
       } 
       selectedCategory = selectedCategory.slice(0,-1); 
       console.log("Selected category :"+ selectedCategory); 
       associativeArray[id] = selectedCategory;    ///inserting values to array 
       var mag_cat_query = magento_categories.find({"categories":selectedCategory}); 

      } 
     }); 

     //updating values of array depends on query results 
     for(prop in associativeArray) 
    { 
     magento_categories.find({"categories":associativeArray[prop]}).exec(function(err,cat){ 
       if(err) 
        return cosole.log(err); 
       cat.forEach(function(c){ 
        console.log("PPPPPPPPPP: "+c.parent_category_ids); 
        associativeArray[prop] = c.parent_category_ids; 
        console.log("p id: "+prop+" Category:: "+associativeArray[prop]); 

       }); 
       return associativeArray; 
      }); 

    } 
     return associativeArray; 
    }).then(function(ass){ 
     console.log("In then"); 
     for(property in associativeArray){ 
      console.log("IDDDD "+property+" Category: "+associativeArray[property]); 
     } 
     res.render('index.html',{products:pro}); 

    }); 

}); 

module.exports = router; 
+0

不應該從mag_cat_query.exec(function(err,cat){})中返回associativeArray; cat.forEach完成後? – user2814599

回答

0

你不應該返回從內mag_cat_query.exec返回associativeArray

for(prop in associativeArray) { 
    mag_cat_query.exec(function(err,cat){ 
    if(err) 
     return cosole.log(err); 

     cat.forEach(function(c){ 
     console.log("PPPPPPPPPP: "+c.parent_category_ids); 
     associativeArray[id] = c.parent_category_ids; 
     console.log("p id: "+id+" Category:: "+associativeArray[id]); 
     }); 

     return associativeArray; 
    }); 
} 
+0

我試過這個解決方案,但它不工作。節點在執行then()塊後更新數組值。 – Sagar