0

I am using nodeJs as a backend language and sql server as a database. I can to delete whole table row from database but can not able to delete specific row. Here is my code...

router.delete('/student/:id', function (req, res, next) {
var connection = new Connection(config);
connection.on('connect', function(err) {
    request = new Request("DELETE FROM student where id=@id", function(err) {
        if (err) { console.log(err);}
    });

    request.addParameter('id');

    request.on('done', function(rowCount, more) {  console.log(rowCount + ' rows returned');   });
    connection.execSql(request);
    res.setHeader('Content-Type', 'application/json');
});

});

1 Answer 1

1

The problem here seems to be the call to addParameter() function with incomplete list of arguments. request.addParameter() expects three arguments, viz. parameter name, data type, value. You only provided the parameter name.

Try changing request.addParameter('id'); to something like this:

var TYPES = require('tedious').TYPES; // Get the TYPES enum.
request.addParameter('id', TYPES.Int, parseInt(req.params.id)); // Assuming your id is an integer. Use proper Types enum, and conversion, if its not.

You can also refer the example on Microsoft website here: https://learn.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.