如何使用 async 和 foreach 遍历数组
How to Iterate over array using async and foreach
我正在尝试创建一个 api,它将在 expressjs 中创建一个包含成员的组。到目前为止它是这样工作的:
使用 req.body
中的 JSON 对象发出 post 请求,我将使用它创建一个新组。如果 req.body
对象的 members 数组包含成员 id,将其添加到组成员数组,否则创建一个新用户,然后将其 id 添加到数组。
基本上只是添加一个现有用户,新用户将被创建然后添加。为此,我需要遍历 req.body
中的成员数组并检查一个对象。
但是下面的代码似乎不能正常工作。我得到 console.log(group_info.members);
的奇怪结果。我希望它在数组中包含 id
的对象,但会得到随机结果。 foreach 循环中似乎有问题。请帮我解决一下。
var express = require('express');
var router = express.Router();
var Group = require('../models/GroupModel');
var User = require('../models/UserModel');
var async = require("async");
router.post('/', function (req, res, next) {
var group_members = [];
var group_info = req.body;
//see if a member object is sent, create user for that else just add the user id to group_members array
async.forEach(group_info.members, function (member, callback) {
if (typeof member == "object") {
//create new user and add the _id to members array
var user = new User(member);
user.save(function (err) {
if (err) return res.status(500).send(err);
var member_object = {id: user._id};
group_members.push(member_object);
}).then(callback);
} else {
var member_object = {id: member };
group_members.push(member_object);
callback();
}
}, function (err) {
//final call back
group_info.members = group_members; //replace the original array in request body with the new array of users
console.log(group_info.members);
var group = new Group(group_info);
group.save(function (err) {
if (err) return res.status(500).send(err);
res.json(group);
});
});
});
var group_members = [];
var group_info = req.body;
var todo = group_info.members.length;
var done = 0;
if(todo == 0)
{
return saveGroup(res, group_members);
}
else
{
group_info.members.forEach(function (member, index, array) {
if (typeof member == "object") {
//create new user and add the _id to members array
var user = new User(member);
user.save(function (err, savedObject) {
if (err || !savedObject)
{
return res.status(500).send(err);
}
else
{
var member_object = {id: savedObject._id};
group_members.push(member_object);
if(++done >= todo)
{
return saveGroup(res, group_info, group_members);
}
}
});
} else {
var member_object = {id: member };
group_members.push(member_object);
if(++done >= todo)
{
return saveGroup(res, group_info, group_members);
}
}
});
}
function saveGroup(res, group_info, group_members)
{
group_info.members = group_members; //replace the original array in request body with the new array of users
console.log(group_info.members);
var group = new Group(group_info);
group.save(function (err) {
if (err) return res.status(500).send(err);
res.json(group);
});
}
看起来你弄错了它的 eachSeries 不是 forEach,所以只需替换 :
async.forEach(group_info.members, function (member, callback)
与:
async.eachSeries(group_info.members, function (member, callback)
更新
正如评论中指出的 forEach 是 async
each
API 的别名,你可以阅读文档 here,谢谢@megawac 指出这一点。
我正在尝试创建一个 api,它将在 expressjs 中创建一个包含成员的组。到目前为止它是这样工作的:
使用 req.body
中的 JSON 对象发出 post 请求,我将使用它创建一个新组。如果 req.body
对象的 members 数组包含成员 id,将其添加到组成员数组,否则创建一个新用户,然后将其 id 添加到数组。
基本上只是添加一个现有用户,新用户将被创建然后添加。为此,我需要遍历 req.body
中的成员数组并检查一个对象。
但是下面的代码似乎不能正常工作。我得到 console.log(group_info.members);
的奇怪结果。我希望它在数组中包含 id
的对象,但会得到随机结果。 foreach 循环中似乎有问题。请帮我解决一下。
var express = require('express');
var router = express.Router();
var Group = require('../models/GroupModel');
var User = require('../models/UserModel');
var async = require("async");
router.post('/', function (req, res, next) {
var group_members = [];
var group_info = req.body;
//see if a member object is sent, create user for that else just add the user id to group_members array
async.forEach(group_info.members, function (member, callback) {
if (typeof member == "object") {
//create new user and add the _id to members array
var user = new User(member);
user.save(function (err) {
if (err) return res.status(500).send(err);
var member_object = {id: user._id};
group_members.push(member_object);
}).then(callback);
} else {
var member_object = {id: member };
group_members.push(member_object);
callback();
}
}, function (err) {
//final call back
group_info.members = group_members; //replace the original array in request body with the new array of users
console.log(group_info.members);
var group = new Group(group_info);
group.save(function (err) {
if (err) return res.status(500).send(err);
res.json(group);
});
});
});
var group_members = [];
var group_info = req.body;
var todo = group_info.members.length;
var done = 0;
if(todo == 0)
{
return saveGroup(res, group_members);
}
else
{
group_info.members.forEach(function (member, index, array) {
if (typeof member == "object") {
//create new user and add the _id to members array
var user = new User(member);
user.save(function (err, savedObject) {
if (err || !savedObject)
{
return res.status(500).send(err);
}
else
{
var member_object = {id: savedObject._id};
group_members.push(member_object);
if(++done >= todo)
{
return saveGroup(res, group_info, group_members);
}
}
});
} else {
var member_object = {id: member };
group_members.push(member_object);
if(++done >= todo)
{
return saveGroup(res, group_info, group_members);
}
}
});
}
function saveGroup(res, group_info, group_members)
{
group_info.members = group_members; //replace the original array in request body with the new array of users
console.log(group_info.members);
var group = new Group(group_info);
group.save(function (err) {
if (err) return res.status(500).send(err);
res.json(group);
});
}
看起来你弄错了它的 eachSeries 不是 forEach,所以只需替换 :
async.forEach(group_info.members, function (member, callback)
与:
async.eachSeries(group_info.members, function (member, callback)
更新
正如评论中指出的 forEach 是 async
each
API 的别名,你可以阅读文档 here,谢谢@megawac 指出这一点。