NODE.js working with update query at Server end - node.js

QUESTION:
I am writing a node.js code on server which accepts multiple values from client on certain event
var table = data['table'];
var columnName = data['colName']
var columnValue = data['colValue']
var primary_id = data['pid']
var updateQuery = "UPDATE "+table+" SET "+columnName+"=? WHERE primary_id="+primary_id;
var query = conn.query(updateQuery, [columnValue] , function (err, result) {
if (err) throw err;
console.log('changed ' + result.changedRows + ' rows');
});
console.log(query.sql);
// This shows exact query which I wanted to RUN against my MySQL db and also executes successfully on my DB if I try to run this manually.
Problem:
Query formed is correct still that query is not executing through
NODE server.
NODE not showing any error thrown on anonymous call back function and not even result.
NOTE : I have tried to RUN simple select through NODE and that works perfectly [all inclusion are are made correctly for mysql
module and its connection object]
It would be great if some body put some lights on further how to debug this in terms of what kind of error its getting in back-end.

Its was silly mistake ... I was destroying connection each time.
So due to asynchronous nature of JS which indeed used in NODE execution was flowing down with out stopping for query to execute.
..Yes offcourse there is possibly option in NODE to use synchronous nature.
but for now when I removed that line it works like a charm.
//conn.destroy();

Related

Can't create MongoDb connection with 2.2.10 Node.js driver

Trying to open connection (using mongodb native driver 2.2.10, and mongoose 4.6.3).
Getting exception:
must pass in valid bson parser
Looks like this is a connection.js line 55.
The same error occurs:
1. for `mongodb` client fails on `MongoClient.connect`.
2. for `mongoose` it fails on startup (before any code execution).
According to documentation I don't need to pass any value to bson options field.
I get the same error. Seems that from v2.2.0 it occurs when you do not close the connection, even if the value returned in Promise.
From docs updateOne method (focus on db.close()):
// Example of a simple updateOne operation using a Promise.
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// Get the collection
var col = db.collection('update_one_with_promise');
col.updateOne({a:1}
, {$set: {a:2}}
, {upsert:true}).then(function(r) {
test.equal(1, r.matchedCount);
test.equal(1, r.upsertedCount);
// Finish up test
db.close();
});
});
I do not know why so did very uncomfortable with Promise to pass also the db.
I rolled back to v2.1.21

Error handling in node-sqlite3

How do I handle error in sqlite3? For example, I have this simple code:
var stmt = db.prepare("update Tickets set " + columns + " where id = (?)");
stmt.run(req.body.id);
stmt.finalize();
db.close();
All four functions prepare, run, finalize, close has the potential to throw error. This is on my express.js server so I'm trying to put a res.error() statement somewhere to return result. I don't want to put it in all of them because I can run into a multiple res.setHeader error.
Is there a document on error handling practice with sqlite3? I can't find it in its API documentation.
Take a look at the api. Each of those functions takes a callback, whose first parameter is an error.
This will help you handle an error, but it will not stop your app from crashing. In order to stop a crash, you'll have to use a try/catch , or preferably learn how to use domains.
Errors are emitted on "error" event.
you may want to try doing something like below after you initialise your db handle.
db.on("error", function(error) {
console.log("Getting an error : ", error);
});

`TypeError: pool.getAll is not a function` when connecting to MongoDB

I am writing an API with Node and Express, and use MongoDB as the database.
It has worked for a long time now, but today when I wrote some changes to a model, the server won't start anymore. I originally thought that a recent code change caused it, but even if I stash my changes and reset to a working state, the error occurs.
This is the error:
TypeError: pool.getAll is not a function
at MongoCR.auth (/location/of/project/node_modules/mongodb/node_modules/mongodb-core/lib/auth/mongocr.js:56:26)
at Server.auth (/location/of/project/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1019:40)
at ReplSet.auth (/location/of/project/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/replset.js:572:17)
at ReplSet.auth (/location/of/project/node_modules/mongodb/lib/replset.js:438:23)
at authenticate (/location/of/project/node_modules/mongodb/lib/db.js:1319:21)
at Db.authenticate (/location/of/project/node_modules/mongodb/lib/db.js:1356:44)
at /location/of/project/node_modules/mongodb/lib/mongo_client.js:414:25
at /location/of/project/node_modules/mongodb/lib/db.js:221:5
at connectHandler (/location/of/project/node_modules/mongodb/lib/replset.js:335:7)
at g (events.js:260:16)
If I comment out all database connections the app runs. I've tried changing database, but it does not work either. I found this and this question and answer, and double checked versions and that all db-calls have callback-methods. I've used the last argument as the callback for calls that affect one entry, and toArray(function(err, result){}) when there are multiple entries returned.
Mongo version is 2.6.3, and I use the Node-mongodb driver.
I connect with
var mongo = require('mongodb').MongoClient;
var url = process.env.MONGODB || 'mongodb://localhost:27017/test';
var myCollection;
mongo.connect(url, function(err, db) {
if(err) console.log(err);
myCollection = db.collection('myCollection');
});
Then I have methods for e.g. find:
myCollection.find({}).toArray(function(err, result){
// work with result
});
I'm lost - thanks in advance.

Why isn't my Q promise working?

I'm new to promises and Q, and I'm trying to convert a route that uses query in node-mysql. Here are excerpts from my code:
var Q = require('q');
// connection defined elsewhere
router.get('/', function(req, res) {
var query = Q.denodeify(connection.query);
var promise = query("-- query ommitted --", [req.user.id]);
promise.then(console.log, console.error);
});
I'm trying to convert this from an existing set up that doesn't use promises, so I know the connection is set up properly and the query is valid. Whenever I try to request this route, I get the same message in stderr:
[TypeError: Cannot read property 'connectionConfig' of undefined]
I don't know where it's coming from so I'm not sure how to find the full stacktrace. I also tried an alternate version of this code where I had my own function instead of console.log and console.error, but this function was never called and the same error appeared.
Updated answer:
You're probably losing the lexical scope to connection when you denodeify the query method.
Looking at the Q documentation it says this can be an issue:
If you are working with methods, instead of simple functions, you can easily run in to the usual problems where passing a method to another function—like Q.nfcall—"un-binds" the method from its owner.
To fix this try using Q.nbind instead:
var query = Q.nbind(connection.query, connection);
var promise = query("-- query ommitted --", [req.user.id]);
promise.then(console.log, console.error);
Original answer:
Looking at the node-mysql source, the only place connectionConfig is accessed is in Pool.js. So my guess would be that you've got an issue with how the pool is being configured.

Meteor client synchronous server database calls

I am building an application in Meteor that relies on real time updates from the database. The way Meteor has laid out the examples is to have the database call under the Template call. I've found that when dealing with medium sized datasets this becomes impractical. I am trying to move the request to the server, and have the results passed back to the client.
I have looked at similar questions on SA but have found no immediate answers.
Here is my server side function:
Meteor.methods({
"getTest" : function() {
var res = Data.find({}, { sort : { time : -1 }, limit : 10 });
var r = res.fetch();
return (r);
}
});
And client side:
Template.matches._matches = function() {
var res= {};
Meteor.call("getTest", function (error, result) {
res = result;
});
return res;
}
I have tried variations of the above code - returning in the callback function as one example. As far as I can tell, having a callback makes the function asynchronous, so it cannot be called onload (synchronously) and has to be invoked from the client.
I would like to pass all database queries server side to lighten the front end load. Is this possible in Meteor?
Thanks
The way to do this is to use subscriptions instead of remote method calls. See the counts-by-room example in the docs. So, for every database call you have a collection that exists client-side only. The server then decides the records in the collection using set and unset.

Resources