I'm writing a node.js script to write in a MongoDB database some data from a MySQL database. It is not a web application, but a local script (both my MySQL and Mongo are local).
The problem is that when all data is written, the script doesn't terminate. I have to close it with CTRL+C. But all my data is written in my MongoDB database.
I've added a "process.exit()" line at the end of the callback called after writing in MongoDB. The app terminates, but no data is written ! And I don't understand why.
Here is the relevant portion of code
(...)
var req = "SELECT * FROM geometry";
// "connection" is a MySQL connection
connection.query(req, function(err, rows, fields) {
console.log("number of lines :" + rows.length);
connection.end();
if (err) {
throw err;
}
// "Entry" is my model
for (var i = 0; i < rows.length; i++) {
Entry.create({
"code" : rows[i].code,
"name" : rows[i].name,
"coords" : rows[i].coords,
"type" : rows[i].type
}, function(err, doc){
if (err) {
throw (err);
}
});
}
console.log("end");
process.exit(); // no data written ! why ?
});
Can you help me ? Thanks in advance.