如何使async.each等待.save()完成?

我正在尝试使用mongoose将一些数据保存到我的collections中。

//tried with async each async.each(indexes, function(index, callback) { newChampion.ID = champions[index].id; newChampion.Key = champions[index].key; newChampion.Name = champions[index].name; newChampion.Title = champions[index].title; Champion.addChampion(newChampion, function(err) { if (err) { console.log(err.message); callback(); } else { callback(); } }); 

问题是,它只推动了对应于最后一个索引的值(133个值中的一个)。 我有唯一的ID,所以这就是为什么在数据库中只有一个值被保存。 我把一个console.log放入addChampion函数,133次我看到相同的值。 添加下面添加的冠军片段:

 module.exports.addChampion = function(newChampion, callback) { newChampion.save(callback); } 

我如何解决这个问题,为了把所有的133值推入到数据库?

与我的本地mysqlasynchronous作业:

 let async = require('async'); let mysql = require('mysql'); var conn = mysql.createConnection({ host: 'localhost', user: 'root', password: 'xxxxx', database: 'xxxxx', port: 3306 }); var sqls = [ 'select id from record where id = 1', 'select id from record where id = 2', 'select id from record where id = 3', 'select id from record where id = 4', 'select id from record where id = 5' ]; async.each(sqls, function(sql, callback) { console.log(sql); conn.query(sql, function (err, res) { console.log(res); }); }); ### output ### select id from record where id = 1 select id from record where id = 2 select id from record where id = 3 select id from record where id = 4 select id from record where id = 5 [ RowDataPacket { id: 1 } ] [ RowDataPacket { id: 2 } ] [ RowDataPacket { id: 3 } ] [ RowDataPacket { id: 4 } ] [ RowDataPacket { id: 5 } ] 

简单的情况下,不是一个真正的asynchronous工作:

 let async = require('async'); let addChampion = function(newChampion, callback) { console.log(newChampion) } indexes = [1, 2, 3]; async.each(indexes, function(index, callback) { newChampion = {}; newChampion.ID = index; addChampion(newChampion, function(err) { if (err) { console.log(err.message); } }) }); ### output ### { ID: 1 } { ID: 2 } { ID: 3 } 

在通过addChampion函数之前,你会检查你的addChampion吗? 它真的有线你可以在它的控制台相同的索引。