Syntax error when using sequelize postgres - node.js

sequelize: 1.7.0
pg: 2.11.1
posgreSql: 9.3 (pg server on my pc)
I have app that is using following config:
db: 'mysql://user:pass#localhost/dbname'
And it works with sequelize and mysql dialect.
When I change it to:
db: 'postgres://user:pass#localhost/dbname'
and dialect to postgres and try to sequelize.sync() I get following error:
{
"message":"syntax error at or near \"(\"",
"name":"error",
"stack":"error: syntax error at or near \"(\"\n at Connection.parseE (/Users/me/Desktop/node/myapp/node_modules/pg/lib/connection.js:561:11)\n at Connection.parseMessage (/Users/me/Desktop/node/myapp/node_modules/pg/lib/connection.js:390:17)\n at [object Object].<anonymous> (/Users/me/Desktop/node/myapp/node_modules/pg/lib/connection.js:92:20)\n at EventEmitter.emit (events.js:95:17)\n at Socket.<anonymous> (_stream_readable.js:746:14)\n at EventEmitter.emit (events.js:92:17)\n at emitReadable_ (_stream_readable.js:408:10)\n at emitReadable (_stream_readable.js:404:5)\n---------------------------------------------\n at module.exports.CustomEventEmitter.failure.CustomEventEmitter.fail.CustomEventEmitter.error (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:93:10)\n at exec (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:92:14)\n at onError (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:72:11)\n at EventEmitter.emit (events.js:95:17)\n at module.exports.CustomEventEmitter.emit (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:61:33)\n at [object Object].<anonymous> (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:131:17)\n---------------------------------------------\n at module.exports.CustomEventEmitter.failure.CustomEventEmitter.fail.CustomEventEmitter.error (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:93:10)\n at exec (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:92:14)\n at onError (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/query-chainer.js:72:11)\n at EventEmitter.emit (events.js:95:17)\n at module.exports.CustomEventEmitter.emit (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:61:33)\n at [object Object].<anonymous> (/Users/me/Desktop/node/myapp/node_modules/sequelize/lib/emitters/custom-event-emitter.js:131:17)",
"code":"42601"
},
"msg":"uncaughtException",
"time":"2014-03-31T14:17:29.496Z",
"v":0
}
What I am doing wrong?
Before I created dbname database in pg there was error missing database so I guess sequelize is connected to pg but it generates some syntax errors in pgsql code??
the problem was that in model schema I had Sequelize.INTEGER(11) - and postgres does not allow to pass 11 as a size of INTEGER - this will be solved in 2.x.x version of Sequelize (You can find issue corresponding to this problem on sequelize github repo).

Sequelize and postgres work well for me. Did you set dialect, protocol, port correctly in config.js like this
{
"development":{
"username":"name",
"password":"pass",
"database":"db",
"dialect":"postgres",
"protocol":"postgres",
"host":"127.0.0.1",
"port":"5432"
},
...

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)

MongoError: write EPIPE

I'm using Node.JS + Mongoose + MongoDB. Had my app working ok until now, now when trying to save a document:
Saving operation:
doc.save(function(err, d){
console.log(err,d);
{"name":"MongoError","message":"write EPIPE"}
Error stack:
{ [MongoError: server localhost:27017 received an error {"name":"MongoError","message":"write EPIPE"}]
name: 'MongoError',
message: 'server localhost:27017 received an error {"name":"MongoError","message":"write EPIPE"}' } undefined
error: TypeError: Cannot read property 'url' of undefined
at EventEmitter.<anonymous> (/home/ec2-user/c/index.js:160:61)
at EventEmitter.<anonymous> (/home/ec2-user/c/node_modules/mongoose/node_modules/mpromise/lib/promise.js:174:48)
at EventEmitter.emit (events.js:95:17)
at Promise.safeEmit (/home/ec2-user/c/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
at Promise.reject (/home/ec2-user/c/node_modules/mongoose/node_modules/mpromise/lib/promise.js:109:15)
at Promise.error (/home/ec2-user/c/node_modules/mongoose/lib/promise.js:94:15)
at Promise.resolve (/home/ec2-user/c/node_modules/mongoose/lib/promise.js:112:24)
at /home/ec2-user/c/node_modules/mongoose/lib/document.js:1555:39
at handleError (/home/ec2-user/c/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:40:22)
at next_ (/home/ec2-user/c/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:75:26)
Looks like I'm able to read data, but not to write new documents. Mongo is app and running. I've tried so far, restarting the DB, with no good results.
Just tried to insert from mongo client and worked
> db.mysecretcollection.insert({url:"sampletestremoveme"});
WriteResult({ "nInserted" : 1 })
So it seems to be a Node.js / Mongoose issue.
It seems to me that the document was too big, actually I was able to save it after removing one of its fields. The weird thing is that mongoose should catch that with "Error: Document exceeds maximum allowed bson size of XXX bytes" exception, but that was never thrown.

Sequelize with postgres defaults to mysql

I'm setting up Sequelize to manage postgres in a Node.js app.
I've required sequelize-postgres, as well as declared 'postgres' in the dialect attr when instantiating a new Sequelize object. But for some reason, it defaults to using mysql.
Exact error message:
/Users/...project/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:306
var connection = mysql.createConnection(connectionConfig);
^
TypeError: Cannot call method 'createConnection' of undefined
at module.exports.connect (.../node_modules/sequelize/lib/dialects/mysql/connector-manager.js:306:28)
at Object.pool.Pooling.Pool.create
(.../node_modules/sequelize/lib/dialects/mysql/connector-manager.js:131:19)
at createResource (.../node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:258:13)
at dispense (.../node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:250:9)
at Object.me.acquire (.../node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:316:5)
at null.fct (.../node_modules/sequelize/lib/dialects/mysql/connector-manager.js:241:19)
at null.<anonymous> (.../node_modules/sequelize/lib/emitters/custom-event-emitter.js:24:18)
at processImmediate [as _immediateCallback] (timers.js:336:15)
Any help is appreciated thank you!

Using compound, I got an error on update when using validatesPresenceOf

I'm creating an app with compound and mongodb. Compound use jugglingdb and jugglingdb-mongodb to connect to mongodb.
I generated a crud for a client using the scaffolding of compound. Then, I added Client.validatesPresenceOf('name'); to app/models/client.js
module.exports = function (compound, Client) {
Client.validatesPresenceOf('name');
};
On create, it work, but on update it fail.
I also tried adding the validation to db/schema.js, but I got the same error.
This is the error I got
/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/connection/server.js:529
throw err;
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:708:11)
at ServerResponse.res.setHeader (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/express/node_modules/connect/lib/patch.js:59:22)
at ServerResponse.res.set.res.header (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/express/lib/response.js:518:10)
at ServerResponse.res.location (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/express/lib/response.js:649:8)
at ServerResponse.res.redirect (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/express/lib/response.js:691:8)
at Controller.Helpers.redirect (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/compound/node_modules/kontroller/lib/helpers.js:168:14)
at eval (eval at (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/compound/node_modules/kontroller/lib/base.js:157:17), :97:38)
at Object.html (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/compound/node_modules/kontroller/lib/helpers.js:43:17)
at eval (eval at (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/compound/node_modules/kontroller/lib/base.js:157:17), :93:20)
at Controller.respondTo (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/compound/node_modules/kontroller/lib/helpers.js:38:5)
at action.cliente.destroy.format.json.send.code (eval at (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/compound/node_modules/kontroller/lib/base.js:157:17), :84:9)
at ModelConstructor. (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb/lib/model.js:827:33)
at ModelConstructor.next (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb/lib/hooks.js:48:18)
at ModelConstructor. (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb/lib/model.js:826:38)
at ModelConstructor.next (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb/lib/hooks.js:48:18)
at /Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb/lib/model.js:825:30
at /Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/lib/mongodb.js:249:9
at /Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/collection.js:703:14
at null. (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/db.js:1709:18)
at g (events.js:175:14)
at EventEmitter.emit (events.js:106:17)
at Server.Base._callHandler (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/connection/base.js:130:25)
at /Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/connection/server.js:464:24
at MongoReply.parseBody (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:132:5)
at null. (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/connection/server.js:410:22)
at EventEmitter.emit (events.js:95:17)
at null. (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:190:13)
at EventEmitter.emit (events.js:98:17)
at Socket. (/Users/camilo/Documents/Pruebas/Nodejs/interno2/node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/connection/connection.js:382:22)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket. (_stream_readable.js:699:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:382:10)
at emitReadable (_stream_readable.js:377:5)
at readableAddChunk (_stream_readable.js:142:7)
at Socket.Readable.push (_stream_readable.js:112:10)
at TCP.onread (net.js:511:21)
Versions:
nodejs: 0.10.1
compoundjs: 1.1.5-21
jubblingdb: 0.2.0-27
jugglingdb-mongodb: 0.0.4-1
The problem was a bug on jubblingdb 0.2.0-27. The author updated it to 0.2.0-28 and the problem was solved.

Reconnect MongoDB using MongoSkin ( Nodejs )

I have been using MongoSkin( Nodejs ) plugin to connect Mongodb as below.
var db = mongo.db(dbconfig.mongo_ip+'/'+dbconfig.mongo_db_name);
db.collection('myprofile').findOne({_id:memberid}, function(err, session){
if(err){
db.close();
console.log(err.stack);
}
})
If i got the mongo connection error from node.js, then how do i reconnect the same using mongoskin,
Mongo Connect errr: Error: failed to connect to [localhost:27017]
at [object Object].<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/server.js:184:51)
at [object Object].emit (events.js:45:17)
at [object Object].<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:110:14)
at [object Object].emit (events.js:45:17)
at Socket.<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/connection.js:301:10)
at Socket.emit (events.js:42:17)
at Array.33 (net.js:799:27)
at EventEmitter._tickCallback (node.js:108:26)
Please help me on this.
A good place to start is
https://github.com/kissjs/node-mongoskin/blob/master/test/db.js
but I assume you need to call open on db under mongoskin as well as in the driver.

Resources