AmishJohn81 AmishJohn81 - 5 months ago 10
Node.js Question

How to get a 'done' message upon completion of bulk insert?

I am trying to run a function upon the completion of a bulk insert request using Node.js, the mssql library, and SQL Server:

sql.connect(config, function (err) {
// error handling

var request = new sql.Request()
request.bulk(table, function (err, rowCount) {
if (err) {
// error handling
}
})
request.on('error', function (err) { // Doesn't Register
console.dir(err)
})
request.on('done', function (returnValue, affected) { // Doesn't Register
MyClass.MyFunction() // FUNCTION I NEED TO CALL
})
})
sql.on('error', function (err) {
// error handling
sql.close()
})


Neither the error, nor the done event is triggered. I need to run
MyFunction
only after the SQL request is finished. Why is it not triggering the event?

Answer

Use callback on bulk function, as described in documentation:

request.bulk(table, function (err, rowCount) {
    if (err) {
        // error handling
    } else {
        MyClass.MyFunction();
    }
});