Node.js异步数组保存数据到mongodb
node.js async array save data to mongodb
我有一个字符串
var my_str = "Jenny [id:51], david, Pia [id:57], Aston [id:20], Raj, ";
我将此发送到函数convert_to_array(my_str),并希望这样的东西返回
[all: [51, 72, 57, 20, 73], new: [72, 73]]
这里,72 &73个新插入的文档到mongodb数据库。
这就是我正在做的:
function convert_to_array(text) {
if(text && text !== '') {
var text_arr = text.split(', ');
text_arr.pop();
var arr = new Array();
var new_profiles = new Array();
var all_profiles = new Array();
for(var i = 0; i < text_arr.length; i++) {
var pair = text_arr[i].split('[id:');
// Its already existing, just add it to the array
if(pair[1]) {
all_profiles.push(pair[1].split(']')[0]);
// Else create a new profile and pass the _id
} else {
// Save to db first
var profileObj = new Profile({name: pair[0], automated: true});
profileObj.save(function(err, data) {
if(err) throw err;
all_profiles.push(String(data._id));
new_profiles.push(String(data._id));
});
}
}
arr = {all: all_profiles, new: new_profiles};
return arr;
}
}
使用这段代码,我只得到这个(或类似的东西,我不记得确切的输出)
[all: [51, 57, 20], new: []]
项目保存在数据库中,我可以看到。但由于node本质上是非阻塞的,因此for循环在数据保存到数据库&并将id推入数组。我尝试了async,但仍然无法解决这个问题。
我添加了一些控制台。查看它的执行情况,如下所示:
yes existing: 51
oh! doesnt exist. creating: david
yes existing: 57
yes existing: 20
oh! doesnt exist. creating: Raj
GET /page/delete_later 200 4ms
Ok created now: 72
Ok created now: 73
我很困惑如何编码它的节点友好!
您需要更改您的convert_to_array
函数,以便在结果完成时调用回调,而不是返回结果返回值。
function convert_to_array(text, callback) {
// do some stuff, call this when done:
// callback(null, arr);
// or this on error:
// callback(err);
}
现在你的结果准备好了吗?当所有text_arr
项都被处理时(即所有对profileObj.save
的调用都完成)。可能在代码中表达这一点的最简单方法是使用async模块(npm install async
):
var async = require('async');
// ...
function convert_to_array(text, callback) {
if(text && text !== '') {
var text_arr = text.split(', ');
text_arr.pop();
var arr = new Array();
var new_profiles = new Array();
var all_profiles = new Array();
async.eachSeries(text_arr, function(it, done) {
var pair = text_arr[i].split('[id:');
// Its already existing, just add it to the array
if(pair[1]) {
all_profiles.push(pair[1].split(']')[0]);
next(); // !important! tell async we are done
// Else create a new profile and pass the _id
} else {
// Save to db first
var profileObj = new Profile({name: pair[0], automated: true});
profileObj.save(function(err, data) {
if(err) {
// throw err; // use callback instead of throw for error handling
next(err);
return;
}
all_profiles.push(String(data._id));
new_profiles.push(String(data._id));
next(); // !important! tell async we are done
});
}, function(err) {
arr = {all: all_profiles, new: new_profiles};
callback(err, arr);
}
} else {
callback(null, undefined); // alter depending on your requirements
}
}
相关文章:
- 在Jade中循环并显示来自Mongodb的数据
- 将数据推送到MongoDB,而无需在MEAN中编辑整个条目
- 如何使用Node.js/MongoDB在AngularJS应用程序中获取特定于目标的数据
- 使用mongoose将数据推送到mongoDB模式中的数组中
- 尝试通过angularjs和node将用户数据发送到mongodb
- 为什么我从一个空的mongodb数据库中获取数据
- 无法使用streams/highland.js从mongodb的结果中获取数据
- 如何在node.js中从mongodb推送请求的数据
- 无法将数据存储到 mongoDB
- Mongodb,通过具有多个条件的_id快速查找数据
- Meteor+MongoDB:如何获取嵌套数据
- 如何在HTML中用两个预定义的值绑定选择选项,以在mongodb中插入数据
- 将数据插入数据库express/mongodb应用程序时出错
- 将MongoDB中的数据返回到jade视图
- MongoDB赢得't第一次返回正确的数据
- MongoDB 在保存数据时抛出错误(拓扑已损坏)
- 将JSON数据存储从Node.js到MongoDB
- 如何在没有特定要求的情况下将数据保存在mongodb中
- Node.js向/从MongoDB发送/返回纯文本数据的最基本方法是什么
- 如何在node.js应用程序中的mongodb中按层次结构顺序插入数据