Count method throws syntax error when used with where clause in loopback - node.js

I am using loopback/strong loop in node.js.
I am trying to use where clause in count method for paginition purpose. Whenever I try to use where clause, it gives me a mysql syntax error for as simple query as below.
Ride.count({
where:{"id":20}
},function(err,totalCount){
if (err) {
log.info("Total error ", err);
fn(err);
}else {
log.info("Total count ", totalCount);
}
});
this is the error I get.
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '20' at line 1\n at Query.Sequence._packetToError (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)\n at Query.ErrorPacket (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\sequences\Query.js:83:18)\n at Protocol._parsePacket (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\Protocol.js:271:23)\n at Parser.write (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\Parser.js:77:12)\n at Protocol.write (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\Protocol.js:39:16)\n at Socket. (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\Connection.js:92:28)\n at Socket.emit (events.js:107:17)\n at readableAddChunk (_stream_readable.js:163:16)\n at Socket.Readable.push (_stream_readable.js:126:10)\n at TCP.onread (net.js:529:20)\n --------------------\n at Protocol._enqueue (D:\Code\liftee\rest-services\node_modules\loopback-co

Try without using where clause. I have tested this on my model and it works.
Ride.count(
{"id":20},
function(err,totalCount){
if (err) {
log.info("Total error ", err);
fn(err);
}else {
log.info("Total count ", totalCount);
}
}
);
Have a look at the strongloop API explorer. You only need to pass value to count function.

Related

Line number error does not show up when using MSSQL Node?

I have been using MySQL database with Nodejs for a while. I recently switched to MSSQL with Nodejs
When using a try catch block with Nodejs and Mysql any error with the query execution would give me the line at which the error occured
So while using MySQL I did:
try{
await pool.quer(`sql statement`)
}
catch(err){
console.log(err)
}
This would give me an error and the line number of error. But with node-mssql, I get:
{ RequestError: Incorrect syntax near 'Invalid'.
at StreamEvents.req.once.err (C:\Users\David\Main Web\node_modules\mssql\lib\msnodesqlv8\request.js:463:17)
at Object.onceWrapper (events.js:277:13)
at StreamEvents.emit (events.js:189:13)
at errors.forEach.err (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\reader.js:33:20)
at Array.forEach (<anonymous>)
at routeStatementError (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\reader.js:26:14)
at invokeObject.end (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\reader.js:258:13)
at freeStatement (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\driver.js:160:13)
at cppDriver.freeStatement (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\driver.js:150:11)
code: 'EREQUEST',
originalError:
{ Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'Invalid'. sqlstate: '42000', code: 102 },
name: 'RequestError',
number: 102,
state: undefined }
This is such an unhelpful error. I know where the error is coming from for this error because there is only one sql statement getting executed right now but as my program grows without knowing where the error is coming from it will be hard to debug. Is this an expected behavior of node-mssql?
If you see the documentation here, it clearly mentions what it return in error and what not.
It clearly mentions that "Those errors are initialized in node-mssql module and its original stack may be cropped. You can always access original error with err.originalError"
In your catch block you can try printing the stack trace. That should give you the line number where the error occurred. You can use one of the following statements:
console.stack("---TRACE---")
OR
var stackTrace = new Error().stack
console.log(stackTrace)

Using mariadb nodeJS connector with multiple queries

I've got a server running Node.js connecting to a MariaDb database. I'm using the Node.js connector and it's working fine for single queries. However, when I try to do multiple queries it's throwing this error:
{ Error: (conn=8439, no: 1064, SQLState: 42000) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 2' at line 1
sql: SELECT 1; SELECT 2; - parameters:[]
at Object.module.exports.createError (\node_modules\mariadb\lib\misc\errors.js:55:10)
at Packet.readError (\node_modules\mariadb\lib\io\packet.js:506:19)
at Query.readResponsePacket (\node_modules\mariadb\lib\cmd\resultset.js:47:28)
at PacketInputStream.receivePacket (\node_modules\mariadb\lib\io\packet-input-stream.js:73:9)
at PacketInputStream.onData (\node_modules\mariadb\lib\io\packet-input-stream.js:129:20)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
fatal: false,
errno: 1064,
sqlState: '42000',
code: 'ER_PARSE_ERROR' }
The current test code I'm using is:
conn = await pool.getConnection({multipleStatements: true});
conn.query({
multipleStatements: true,
sql: "SELECT 1; SELECT 2;"
}).then((data: any) => {
console.log(data);
conn.end();
});
The documentation suggests multipleStatements is an option, but looking through the GitHub Repo, the only places I've found it is under lib/config/connection-options.js and the GitHub documentation. I also noticed the batch option but that seems to be specifically for a load of inserts.
Is there a way to use the mariadb-connector-nodejs to run multiple queries in a single query call? If so, what else could I be doing wrong that only multi-statement queries are not working?
Versions:
- MariaDb: 10.1.34-MariaDB
- Node.js: v10.14.2
- mariadb-connector-nodejs: mariadb#2.0.2-rc
multipleStatements is the good option, but as #rolandstarke indicate, that is a connection option.
When using creating pool, you indicate pool + connection options, since pool will handle connection creation. see related documentation
Example:
const mariadb = require("mariadb");
const pool = mariadb.createPool({ multipleStatements: true });
pool.query("select 1; select 2")
.then(results => {
//select 1 results
console.log(results[0]); //{ '1': 1 }
//select 2 results
console.log(results[1]); //{ '2': 2 }
})
.catch(err => {
//handle error
});

The strange Node.js error after starting script

I use Node.js.
My server js script I run such:
node chat_server.js
After I get errors messages in terminal CentOS:
Express server listening on port undefined in development mode.
+ User undefined connected node_redis: no callback to send error: ERR wrong number of arguments for 'sadd' command
/home/who/public_html/node/node_modules/redis/index.js:582
throw err;
^ Error: ERR wrong number of arguments for 'sadd' command
at ReplyParser. (/home/who/public_html/node/node_modules/redis/index.js:317:31)
at ReplyParser.emit (events.js:95:17)
at ReplyParser.send_error (/home/who/public_html/node/node_modules/redis/lib/parser/javascript.js:296:10)
at ReplyParser.execute (/home/who/public_html/node/node_modules/redis/lib/parser/javascript.js:181:22)
at RedisClient.on_data (/home/who/public_html/node/node_modules/redis/index.js:547:27)
at Socket. (/home/who/public_html/node/node_modules/redis/index.js:102:14)
at Socket.emit (events.js:95:17)
at Socket. (_stream_readable.js:748:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:410:10)
Excuse me, but I do not understand the reason of these errors.
On what I should get attention and how fix it?
For example, I use command redis SADD: redis_cli.sadd( "user.friend:" + currentIdUser, data.idUser);
I have done a experiment, created a new text script:
var redis = require("redis");
var client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
client.sadd("users","naveen",function(err,reply){
console.log('Ok');
if(err)
throw err;
return reply;
});
It have given me in console: OK. It mean, that all works fine.
I think in my code:
redis_cli.sadd("userslist", currentIdUser);
the variable currentUser is simply empty or undefined. It gives me the next errors.
Problem was at my script in line:
redis_cli.sadd("userslist", currentIdUser);
The variable is undefined. It calls error redis.
Thank you all for help.

Trouble with reading nodes with node-neo4j & Debugging skills with neo4j-shell

I'm new to Neo4J. So far, I successfully installed and started the Neo4J server and I checked it by running the command neo4j status.
By using node-neo4j driver to add & update nodes to the database.
In my nodejs server, I create a new database:
db = new neo4j("http://127.0.0.1:7474");
Next, I insert a new node:
db.insertNode( {"name": "Darth Vader","sex": "male"}, (err, node) ->
if err then throw err
console.log "Insert node"
console.log node
)
I face no error when inserting a new node. However, when I try to read this node
db.readNode( {"name": "Darth Vader"}, (err, node) ->
if err then throw err; # 48th line of server.js
console.log "Read node"
console.log node
)
ReadNode function throws the following exception at 48th line( you can find the 48th line at the code snippet given above).
server.js:48
throw err;
^
Error: HTTP Error 500 occurred while reading a node.
at node_modules/node-neo4j/main.js:151:15
at Request.callback (node_modules/node-neo4j/node_modules/superagent/lib/node/index.js:656:3)
at Request.<anonymous> (node_modules/node-neo4j/node_modules/superagent/lib/node/index.js:131:10)
at Request.emit (events.js:95:17)
at IncomingMessage.<anonymous> (node_modules/node-neo4j/node_modules/superagent/lib/node/index.js:802:12)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:929:16
at process._tickCallback (node.js:419:13)
Then, I tried to debug my process by checking my database and tried neo4j-shell and typed dbinfo on the command line, I expected to see my database and the Darth Vader node that already inserted.
However, dbinfo returns nothing at all!
How can I find my databases, and nodes in this database with neo4j-shell?
How can I make sure that I successfully inserted the node? How can I read the node that I already inserted?
Do you have any idea?
Thank you in advance!
To make things clear: There are two node-neo4j version out there:
https://github.com/philippkueng/node-neo4j
https://github.com/thingdom/node-neo4j
You are using the philippkueng version: db.readNode will work with nodeId's only. I think you should use db.cypherQuery() with a cypher statement instead for querying the neo4j database.
For example:
db.cypherQuery('MATCH (n {name: "Darth Vader"}) RETURN n',
function(err, result){
if(err) throw err;
console.log(result.data); // delivers an array of query results
console.log(result.columns); // delivers an array of names of objects getting returned
});
Is you want to use labels and indexes without Cypher to lookup the node you could use this:
// add Darth Vader with the label Person
db.insertNode( {name: 'Darth Vader',sex: 'male'}, 'Person',
function(err, node) {})
db.readNodesWithLabelsAndProperties('Person', {name: 'Darth Vader'},
function (err, result) {})
For debugging as #codewithcheese already mentions use the Neo4j browser at:
http://localhost:7474

Cassandra driver for nodejs Helenus

I am having problems with cassandra driver for nodejs - helenus. I crated function loop that insert records into table, and I am calling with ab tool. The problem is after 10-20 insert the driver throws error:
This is the coffee code:
helenus = require 'helenus'
pool = new helenus.ConnectionPool(
hosts: ["mybalancer:9160"]
keyspace: "stats"
timeout: 90000
)
pool.on "error", (err) ->
console.error err.name, err.message
pool.connect (err, keyspace) ->
if err
throw (err)
else
vals = [uuid.v4(), uuid.v4()]
#insert_statement = "INSERT INTO test2 (id, name) values(%s, %s)"
console.log(vals)
pool.cql insert_statement, vals, (err, results) ->
console.log err, results
And This is the error:
/home/udev/development/project/U/hubber/app/controllers/event_logger_cassandra.js:98
throw err;
^
HelenusNoAvailableNodesException: Could Not Connect To Any Nodes
at replyNotAvailable (/home/udev/development/project/U/hubber/node_modules/helenus/lib/pool.js:16:25)
at onConnect (/home/udev/development/project/U/hubber/node_modules/helenus/lib/pool.js:98:9)
at /home/udev/development/project/U/hubber/node_modules/helenus/lib/pool.js:120:7
at null.<anonymous> (/home/udev/development/project/U/hubber/node_modules/helenus/lib/connection.js:199:5)
at EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (/home/udev/development/project/U/hubber/node_modules/helenus/node_modules/helenus-thrift/lib/thrift/connection.js:56:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:830:16
at process._tickCallback (node.js:415:13)
I have cassandra cluster of three nodes(large servers on amazon) behind load balancer.
I have monitor on the servers and the nodes are always up and work fine.
What do I missing here?
Thanks.

Resources