I want to call a stored procedure from MySql node:
How do I call it? The documentation says:
You can call stored procedures from your queries as with any other mysql driver. If the stored procedure produces several result sets, they are exposed to you the same way as the results for multiple statement queries
I tried searching for it on internet but got very old results which do not work anymore.
I tried:
connection.query('procedure_name()', {84,Bhuwan}, function(err, result) {
connection.destroy();
if (err)
throw err;
callback(err, result);
});
But I am getting error.
Can anyone provide a proper syntax for it??
You have to use 'call' command and if you have parameters to pass to the query, you need to add '?' marks. Check the code.
connection.query("call procedure_name(?,?)", [param1, param2], function (err, result) {
if (err) {
console.log("err:", err);
} else {
console.log("results:", result);
}
});
Related
I am having trouble running multiple queries inside a single connection with RethinkDB. I have tried the r.do as seen in this question, however no success. I have also tried working with the conditional update queries. What I am looking to do is:
Open the connection.
Query to see if my field is there and if it is, perform some tasks.
Query to see if a counts field is there, subtract it by one.
What would be the best way to go about this? It seems I might be missing something?
r.connect(config.rethinkdb, function(err, conn) {
if (err) {
throw err;
}
else {
console.log('Connected.');
app.set('rethinkdb.conn', conn);
}
r.table('upcs').filter({AcceptedUPC:data}).run(conn, (err, cursor) => {
if (err) throw err;
console.log(data);
cursor.toArray((err,resu) => {
if (err) throw err;
//make a csv with some information
})
})
And in the same connection run
r.table('upcs').filter({AcceptedUPC:data}).filter(r.row.hasFields("UPCCount")).update({UPCCount: r.row("UPCCount").sub(1)}).run(conn, (err,cursor) => {
if (err) throw err;
});
Running this in NodeJS
I'm going to assume you are using this library for node.
You can that they actually allow you to do either callbacks or promises. I would recommend promises to avoid brackets of hell.
For promises you can use the bluebird library to make life easy.
You can do it by doing the following.
r.connect(config.rethinkdb).then(() => {
console.log("Connected");
app.set("rethinkdb.conn", conn);
return r.table('upcs').filter({AcceptedUPC:data}).run(conn);
}).then((cursor) => {
console.log(data); //maybe this should be cursor but I don't use rethinkDB
return cursor.toArray();
}).then((resu) => {
//make a csv with some information
}).catch((err) => {
throw err;
});
I am fairly new to node.js, and haven't done much of javascripts. Tried to search my problem, but couldn't find specific answer related to it.
So, while I was working on attaching the PostgreSQL to my app, I followed a snippet from some example on web, and it seems like working pretty well.
Anyways I wanted to understand how it works, I had a problem understanding specific part of the following code:
module.exports = {
query: function(text, values, cb) {
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
console.log(client);
client.query(text, values, function(err, result) {
done();
cb(err, result);
})
});
}
}
and the specific part is:
pool.connect(function(err, client, done) { ... }
What I understood is connect function takes callback function with err, client, and done as parameter, however I couldn't understand from where the function(err, client, done) is passed to connect function as parameter. By where, I mean an object or a caller that call connect function.
I had suspected that it would be handled internally, but I wanted to know clearly.
Bydefault all callback function, the first parameter must be an error and second will be a result of ur callback function.
Done is similar to callback keyword, which says, your task is over and give response back from where the function has called, its just like return statement in normal function
example:
function callbackDemo(arg1, arg2, callback) {
if (condition)
callback(null, "Success");
else
callback("error");
}
callbackDemo(1, 2, function(err, result){
if(!err)
console(result);
else
console.log(err);
});
I was wondering if it's possible to query the database inside of a callback of another query. So for example,
connection.connect();
connection.query(query, [param1, param2], function(err, rows, fields) {
// do something
connection.query(new_query, function(err, rows, fields) {
// do something else
});
});
connection.end();
I am getting a Error: Cannot enqueue Query after invoking quit. error on line 4 of this code. So I was wondering if this is possible at all.
Explanation of your problem:
js is executed line by line. In order, that would be:
connection.connect()
connection.query( params...) (async i/o, placing callbacks)
connection.end()
Then, when a the query to the database is finished, the callback in 2. is executed, but the 3. was executed before, ending connection. So in that point in callback, you cant make another query, because the connection is closed.
Solution:
connection.end() should be inside the last nested query
Fixed code:
connection.connect();
connection.query(query, [param1, param2], function(err, rows, fields) {
// do something
connection.query(new_query, function(err, rows, fields) {
// do something else
connection.end();
});
});
Yes, you can issue queries in callback. You're getting the error because the connection.end call is NOT in a callback. Where you have it now, it will be called before the first callback fires.
i try to use for connection.release();
pool.getconnection(function(err,connection){
if(!err)
{
connection.release();//after this perform query operation
connection.query(query, [param1, param2], function(err, rows, fields) {
// do something
connection.query(new_query, function(err, rows, fields) {
// do something else
});
});
}
})
I'm using this package elsewhere and it's working just fine, however in one particular example with one XML file I'm getting "undefined" errors.
Example:
fs.readFile('./XML/theXMLfile13mb.xml', 'ascii', function(err,data){
if(err) {
console.log("Could not open file " + err);
process.exit(1);
}
parseString(data, function (err, result) {
console.log(result); // Returns undefined
var json1 = JSON.stringify(result); // Gives an error
var json = JSON.parse(json1);
The xml2js docs don't really mention how this might be possible/what this might mean. I've tried using other XML files and they work fine. This particular XML file is no bigger than the others nor does it appear to be any less in-tact (it opens fine in my browser and all the data is presented as expected).
Any ideas on how I could troubleshoot this?
You need to convert the data from a Buffer to a String, use this:
parseString(data.toString(), function (err, result) {
Instead of:
parseString(data, function (err, result) {
i am using mysql felix node.js module.
i am using its pool connection.
i have many queries in my server side (written in node) that are written like this:
this.courtsAmount = function(date, callback){
pool.getConnection(function(err, connection) {
connection.query('SELECT MAX(id) AS result from courts where date="' + date + '"', function(err, rows, fields){
connection.release();
if (err) throw err;
if (rows[0].result)
callback(rows[0].result);
else
callback(0);
});
});
};
for some reason i keep getting this error (from all sorts of functions that are written like this):
Type Error: Cannot call method 'releaseConnection' of null
which is pointing to the 'connection.release()' line.
i really don't understand what is the problem here, as i understand from the API inside the function of pool.getConnection i am supposed to have full access to the connection. maybe it is an issue of something to have to do with timeout of the connection? i believe it is not the case because this error happens while i am actually going through my site.
another question:
do i have to deal with the option that connections will timeout if i use the pool?
and if the answer is yes, how should i do it?
thanks.
I'd recommend adding error checking before attempting to use the connection instance. I've updated your code (see my comments inline):
this.courtsAmount = function(date, callback) {
pool.getConnection(function(err, connection) {
if (err) throw err; // <-- 'connection' might be null, so add an error check here
connection.query('SELECT MAX(id) AS result from courts where date="' + date + '"', function(err, rows, fields) {
if (err) throw err; // <-- moved this line up to check for an error first
connection.release(); // <-- moved this line down so error checking happens first
if (rows[0].result)
callback(rows[0].result);
else
callback(0);
});
});
};
Also, if the date instance is coming from an untrusted source, then your code is vulnerable to SQL injection. You might consider switching to mysql2 and using a prepared statement.