0

I have a azure function that has three query parameters , I want to insert those into Azure SQL db. Below is the code that I wrote, however, I get an error saying invalid column. The select query works fine.

const Connection = require('tedious').Connection;
const Request = require('tedious').Request;

module.exports = function (context, req) {
 context.log('JavaScript HTTP trigger function');
 let bId = req.query.binId
 let bIP = req.query.IPAddress
 let btype =req.query.bincat

 context.log(bId);
 context.log(bIP);
 context.log(btype);

 const config = {
    userName: 'used the necessary input',
    password: 'used the necessary input',
    server: 'used the necessary input',
    options: {
        database: 'used the necessary input',
        encrypt: true
    }
};
if (bId && btype && bIP) {
    context.res = {
        body: "Details: " + (bId) + ","+(bIP)+","+(btype) 
    };
}
else {
    context.res = {
        status: 400,
        body: "Please pass a name on the query string"
    };
}


context.log('config done');
const connection = new Connection(config);
connection.on('connect', err => {
    err ? context.log(err) : executeStatement();
});
context.log('connection done');
const query = 'insert into Bins(BinId, IPAddress, BinType) values (bId,bIP, btype)';
//const query = 'select * from Bins';
const executeStatement = () => {
    const request = new Request(query, (err, rowCount) => {
        context.log('inside exe');
        err ? context.log(err) : context.log(rowCount);
    });
    request.on('row', columns => {
        columns.forEach(column => context.log(column.value));
    context.done();
    });
connection.execSql(request);
};
 context.log('executed statement');
 //context.done();
// context.done();
};

The error is in the insert command.

1 Answer 1

1

You're missing request.addParameter(... for your parameters, also your parameters should start with @. see the documentation for more details https://tediousjs.github.io/tedious/parameters.html

After you create the request object, you need to do somehting like

var TYPES = require('tedious').TYPES

const query = 'insert into Bins(BinId, IPAddress, BinType) values (@bId,@bIP, @btype)';
const request = new Request(query, .....

request.addParameter('bId', TYPES.Int, bId);
request.addParameter('bIP', TYPES.VarChar, bIP);
request.addParameter('btype', TYPES.VarChar, btype);


...
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.