我正在尝试使用mongo db query&更新对象'associativeArray'的数组。想要将它返回到下一个然后阻止。 但是当我运行应用程序时,我在数组中得到旧值。
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 :(得分:0)
你不应该从associativeArray
内返回mag_cat_query.exec
吗?
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;
});
}