yyb_haoren
2017-02-28 10:34nodejs的异步处理问题
5mongoose = require('mongoose'),
async = require('async'),
ThrowSku = mongoose.model('ThrowSku'),
var importCount = 0;
var throwSkus = req.body.throwSkus;
#######1、先全部执行完毕#########
throwSkus.forEach(function (throwSku) {
var throwSkuSchema = new ThrowSku({
skuId: throwSku.skuId,
stockGroup: throwSku.stockGroup,
supplierId: throwSku.supplierId,
department: throwSku.department
});
throwSkuSchema.save(function (err) {
if (err) {
console.log(err);
} else {
importCount++;
}
});
});
#######2、循环完后再返回############
console.log(importCount);
res.jsonp(importCount);
以上是部分代码,想问下怎么用async.forEach()来让程序异步把全部数据都保存到数据库后再让res返回请求。
现在的情况是 假如我传过来的数组长度为2,还在循环的时候就打印出来了个0。
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- 在nodejs使用 process.argv无反应
- node.js
- 1个回答
- nodejs与html代码分离
- javascript
- 编程
- node.js
- 0个回答
- 寻求linux for arm7环境下,nodejs调用.so动态库出错问题的解决方法
- linux
- arm
- 库
- node.js
- raspberry pi
- 2个回答
- nodejs 线上应用问题 请求不到对应资源
- 百度
- node.js
- 3个回答
- nodejs promise 多个foreach返回问题
- promise
- 2个回答
换一换