Sir Rubberduck
Sir Rubberduck

Reputation: 2276

Prepared statement not executing in node js with mssql

After I run the code, all I get is the console log "Done.", with nothing updated in the database. What am I doing wrong?

var pool = new sql.ConnectionPool(config);
var ps = new sql.PreparedStatement(pool);

ps.input('code', sql.VarChar);
ps.input('hex', sql.VarChar);

ps.prepare("UPDATE tHE_SetItem SET acPicture = CONVERT(varbinary(max), @hex, 2) WHERE acIdent = @code;", function(err) {

    async.mapSeries(hexes, function(pair, next) {

        ps.execute({code: pair.code, hex: pair.hex}, next);

    }, function(err) {

        ps.unprepare(function(err) {

            console.log("Done!");

        });
    });
});

Upvotes: 1

Views: 2527

Answers (1)

Sir Rubberduck
Sir Rubberduck

Reputation: 2276

As pointed out in the comment, I left out the error callbacks, which in turn notified me that I was not actually making a connection. Here is the revised code.

var pool = new sql.ConnectionPool(config);
pool.connect().then(function(){ // <------------- This in particular
    var i = 0;
    var ps = new sql.PreparedStatement(pool);
    ps.input('code', sql.VarChar);
    ps.input('hex', sql.VarChar);
    ps.prepare("UPDATE tHE_SetItem SET acPicture = CONVERT(varbinary(max), @hex, 2) WHERE acIdent = @code;", function(err) {
        if(err) console.log(err);
        async.mapSeries(hexes, function(pair, next) {
            i++;
            console.log(i + "/" + hexes.length + " " + Math.round(i / hexes.length * 100) + "% - " + pair.code);
            ps.execute({code: pair.code, hex: pair.hex}, next);
        }, function(err) {
            if(err) console.log(err);
            ps.unprepare(function(err) {
                if(err) console.log(err);
                console.log("Done!");
            });
        });
    });
}).catch(function (err) {
    console.log(err);
});

Upvotes: 1

Related Questions