i'm kinda new to node.js and mongo and i'm having a little problem. I can't bulk insert documents into mongodb using ajax on client and nodejs/express on server. Here is my client code:
<button class="btn>Send</button>
data = {'test':'test1','test':'2','test':'test1','test':'test2','test':'test1','test':'test2'}
$(".btn").click(function(){
$.ajax({
url: 'http://localhost:8000/2',
type: 'post',
dataType: 'json',
data: data
});
});
And here is my route handler in node:
app.post('/2', function(req, res){
var docs = [];
for(var i = 0; i < 10000; i++) {
docs[i] = req.body;
}
db.collection('test', function(err, collection){
collection.insert(docs, function(err, result) {
if (!err) {
console.log(result);
res.end();
} else {
throw err
}
})
});
})
I'm getting data nicely with console.log(docs), but my test collection is empty. What am i missing? Thanks