I am trying to run a Sequelize query against a Postgres DB, as follows:
async getRowFromWorkerTable(contact_uri){
console.log(`running query...`);
let result;
try{
result = await sequelize.query("select * from worker where contact_uri='"+contact_uri+"'",
{ type: sequelize.QueryTypes.SELECT});
}
catch(err){
console.err(err);
}
finally{
console.log(`query complete.`);
}
return result;
}
I get the running query... text, but then no error or query complete.
I initialize sequelize prior to this as follows:
const sequelize=new Sequelize(process.env.database,process.env.username,process.env.password,{
dialect:'postgres',
});
Examining the sequelize object with console.log({sequelize}) gives the following:
{
sequelize: <ref *1> Sequelize {
options: {
dialect: 'postgres',
dialectModule: null,
dialectModulePath: null,
host: 'localhost',
protocol: 'tcp',
define: {},
query: {},
sync: {},
timezone: '+00:00',
clientMinMessages: 'warning',
standardConformingStrings: true,
logging: [Function: log],
omitNull: false,
native: false,
replication: false,
ssl: undefined,
pool: {},
quoteIdentifiers: true,
hooks: {},
retry: [Object],
transactionType: 'DEFERRED',
isolationLevel: null,
databaseVersion: 0,
typeValidation: false,
benchmark: false,
minifyAliases: false,
logQueryParameters: false,
dialectOptions: [Object]
},
config: {
database: 'dbname',
username: 'username',
password: 'password',
host: 'localhost',
port: 5432,
pool: {},
protocol: 'tcp',
native: false,
ssl: undefined,
replication: false,
dialectModule: null,
dialectModulePath: null,
keepDefaultTimezone: undefined,
dialectOptions: [Object]
},
dialect: PostgresDialect {
sequelize: [Circular *1],
connectionManager: [ConnectionManager],
QueryGenerator: [PostgresQueryGenerator]
},
queryInterface: QueryInterface {
sequelize: [Circular *1],
QueryGenerator: [PostgresQueryGenerator]
},
models: {},
modelManager: ModelManager { models: [], sequelize: [Circular *1] },
connectionManager: ConnectionManager {
sequelize: [Circular *1],
config: [Object],
dialect: [PostgresDialect],
versionPromise: [Promise [Object]],
dialectName: 'postgres',
pool: [Pool],
lib: [PG],
nameOidMap: {},
enumOids: [Object],
oidParserMap: Map(0) {}
},
importCache: {}
}
}
I am able to successfully run this query through psql. Why am I not getting any query results through Sequelize?
Figured it out--this issue has been addressed in depth here: https://github.com/sequelize/sequelize/issues/12158
Related
So I have two machines running in the cloud (both Ubuntu 18.04/Node 13/NPM 6.4) and on one machine I am running into issues with Mongo - I cannot figure out why.
Here is my simple script:
const MongoClient = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017/mydb';
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, async (err, client) => {
if(err) throw err;
console.log('Mongo Client: ', client)
const db = client.db('mydb');
console.log('Mongo DB Connected', db)
const userCount = await db.collection('users').countDocuments()
console.log('# of Users: ', userCount)
});
This works as expected on one machine, but on the other I get the following error:
(node:22716) UnhandledPromiseRejectionWarning: TypeError: db.collection(...).countDocuments is not a function
I am hoping there is a way to have MongoClient spit out it's configuration/version information in the script so that I can see some difference between the two??
Right now when the script runs and I log out "client" I see this on the working machine:
Mongo Client: MongoClient {
_events: [Object: null prototype] { newListener: [Function (anonymous)] },
_eventsCount: 1,
_maxListeners: undefined,
s: {
url: 'mongodb://localhost:27017/mydb',
options: {
servers: [Array],
caseTranslate: true,
useNewUrlParser: true,
useUnifiedTopology: true,
checkServerIdentity: true,
sslValidate: true,
dbName: 'mydb',
socketTimeoutMS: 0,
connectTimeoutMS: 10000,
retryWrites: true,
useRecoveryToken: true,
readPreference: [ReadPreference],
promiseLibrary: [Function: Promise]
},
promiseLibrary: [Function: Promise],
dbCache: Map {},
sessions: Set {},
writeConcern: undefined,
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined
},
namespace: MongoDBNamespace { db: 'admin', collection: undefined }
},
topology: NativeTopology {
...
This is different than what I see on the non-working machine:
Mongo Client: Db {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
s: {
databaseName: 'mydb',
dbCache: {},
children: [],
topology: Server {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
s: [Object],
bson: [Getter],
isMasterDoc: [Getter],
poolSize: [Getter],
autoReconnect: [Getter],
host: [Getter],
port: [Getter],
emitOpen: false,
connectTimeoutMS: 30000,
socketTimeoutMS: 0
},
options: {
read_preference_tags: null,
readPreference: [ReadPreference],
url: 'mongodb://localhost:27017/mydb',
promiseLibrary: [Function: Promise],
native_parser: true
},
logger: Logger { className: 'Db' },
bson: BSON {},
authSource: undefined,
readPreference: ReadPreference {
_type: 'ReadPreference',
mode: 'primary',
tags: undefined
},
...
Any guidance would be greatly appreciated.
When all else fails, reboot it. In my case I deleted Mongo and re-installed on the machine not working and now it works.
I have created two modules in my express using sequelize with typescript but when I try to log the model using console.log(require('../models')) it's giving an empty array.
{ models: {},
sequelize:
Sequelize {
options:
{ dialect: 'postgres',
dialectModulePath: null,
host: '127.0.0.1',
protocol: 'tcp',
define: {},
query: {},
sync: {},
timezone: '+00:00',
logging: [Function: bound consoleCall],
omitNull: false,
native: false,
replication: false,
ssl: undefined,
pool: {},
quoteIdentifiers: true,
hooks: {},
retry: [Object],
transactionType: 'DEFERRED',
isolationLevel: null,
databaseVersion: 0,
can someone tell me what I'm doing wrong?
I am trying to deploy my Expressjs application into lambda, but i am getting below error. Please try to me to resolve this error.
lambda response:
{
"statusCode": 502,
"body": "",
"headers": {}
}
lambda log (it contains console of mongo connection and error)
NativeConnection {
base:
Mongoose {
connections: [ [Circular] ],
plugins: [],
models: { Student: [Object] },
modelSchemas: { Student: [Object] },
options: { pluralization: true } },
collections:
{ student:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'student',
collectionName: 'student',
conn: [Circular],
queue: [],
buffer: false,
emitter: [Object] } },
models:
{ Student:
{ [Function: model]
hooks: [Object],
base: [Object],
modelName: 'Student',
model: [Function: model],
db: [Circular],
discriminators: undefined,
schema: [Object],
collection: [Object],
Query: [Object],
'$__insertMany': [Function],
insertMany: [Function] } },
config: { autoIndex: true },
replica: false,
hosts: null,
host: '1.0.0.0.0',
port: 3000,
user: undefined,
pass: undefined,
name: 'sudentdb',
options:
{ mongos: {},
db: { safe: true, forceServerObjectId: false },
auth: {},
server: { socketOptions: {}, auto_reconnect: true },
replset: { socketOptions: {} } },
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
db:
EventEmitter {
domain: null,
_events:
{ close: [Function],
error: [Function],
reconnect: [Function],
timeout: [Function],
open: [Function],
parseError: [Function] },
_eventsCount: 6,
_maxListeners: undefined,
s:
{ databaseName: 'studentdb',
dbCache: {},
children: [],
topology: [Object],
options: [Object],
logger: [Object],
bson: {},
authSource: undefined,
readPreference: undefined,
bufferMaxEntries: -1,
parentDb: null,
pkFactory: undefined,
nativeParser: undefined,
promiseLibrary: [Function: Promise],
noListener: false,
readConcern: undefined },
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
_listening: true },
_events:
{ connected: [Function],
error: [Function],
disconnected: [Function] },
_eventsCount: 3 }
{ [Error: socket hang up] code: 'ECONNRESET' }
student.model.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var studentSchema = new Schema({
name: String,
rollnumber: Number,
},{collection:"student"});
module.exports = mongoose.model('Student', studentSchema);
api/student(REST API - GET method)
exports.index = function(req, res) {
console.log(mongoose.connection)
Student.findOne({},function(err,doc){
if(err){
return res.json("error found");
}else{
return res.json(doc);
}
});
};
lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./bin/www');
const server = awsServerlessExpress.createServer(app);
exports.handler = (event,context)=>{
console.log("EVENT: " + JSON.stringify(event));
awsServerlessExpress.proxy(server,event,context);
}
I'm using node-mongo for querying and inserting documents to a mongoDB database.
This is what I'm assuming for inserting:
collection.insert({}, function(err,insertedDocuments){
});
However for insertedDocuments I expect them to be the actual documents, but this is what insertedDocuments is always looking like:
{"ok":1,"n":1}
The important code to explain what I'm doing is:
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
MongoClient.connect('mongodb://127.0.0.1:27017/places', function (err, db) {
if (err) throw err;
console.log("Connected to database ... Will work with default places collection".inverse);
places = db.collection('places');
strings = db.collection('strings');
app.route('/places').post(newPlaceController);
app.listen(6190, function() {
console.log('Express listening'.inverse);
});
});
function newPlaceController (request,response) {
console.log(request.body);
var latitude = request.body.lat;
var longitude = request.body.long;
var name = request.body.name;
var newPlace = getNewPlaceObject(name,latitude,longitude); // This returns the object/document to be inserted
places.insert(newPlace, function (err,createdPlace) {
if (err)
response.send(JSON.stringify(err),500);
else {
console.log(createdPlace);
response.send(JSON.stringify(createdPlace),200);
}
});
}
Strangely, the log of createdPlace looks like this:
{ result: { ok: 1, n: 1 },
connection:
{ domain: null,
_events:
{ close: [Object],
error: [Object],
timeout: [Object],
parseError: [Object],
connect: [Function] },
_maxListeners: 10,
options:
{ socketOptions: {},
auto_reconnect: true,
host: '127.0.0.1',
port: 27017,
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
disconnectHandler: [Object],
bson: {},
messageHandler: [Function],
wireProtocolHandler: {} },
id: 2,
logger: { className: 'Connection' },
bson: {},
tag: undefined,
messageHandler: [Function],
maxBsonMessageSize: 67108864,
port: 27017,
host: '127.0.0.1',
keepAlive: true,
keepAliveInitialDelay: 0,
noDelay: true,
connectionTimeout: 0,
socketTimeout: 0,
domainSocket: false,
singleBufferSerializtion: true,
serializationFunction: 'toBinUnified',
ca: null,
cert: null,
key: null,
passphrase: null,
ssl: false,
rejectUnauthorized: false,
responseOptions: { promoteLongs: true },
flushing: false,
queue: [],
connection:
{ _connecting: false,
_handle: [Object],
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_maxListeners: 10,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
onend: null,
destroyed: false,
bytesRead: 56,
_bytesDispatched: 215,
_pendingData: null,
_pendingEncoding: '',
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1,
pipe: [Function],
addListener: [Function: addListener],
on: [Function: addListener],
pause: [Function],
resume: [Function],
read: [Function],
_consuming: true },
writeStream: null,
buffer: null,
sizeOfMessage: 0,
bytesRead: 0,
stubBuffer: null },
ops: [ { name: 'OXXO', loc: [Object], _id: 553723a2a2c10c273605309a } ] }
What am I doing wrong?
The new record is at createdPlace.ops[0]. What you are calling createdPlace is not the new document, but a wrapper object the API docs call result. It contains metadata about the operation and then under the ops property array you can find the new documents.
Here's an excerpt from the documentation
The insert command will return a results object that contains several fields that might be useful.
result Contains the result document from MongoDB
ops Contains the documents inserted with added _id fields
connection Contains the connection used to perform the insert
You might consider a more-convenient module such as monk which will return the document to you. (Also recommended because in general the mongodb native API is so bad it's practically developer-hostile).
I am interested in writing a mongoose plug in to make all fields required. I know there are other ways to do this, but I like the idea of writing my own plug in.
From docs http://mongoosejs.com/docs/plugins:
// game-schema.js
var lastMod = require('./lastMod');
var Game = new Schema({ ... });
Game.plugin(lastMod, { index: true });
but when I create a model from my schema and look at the properties, I don't see a plugin() method:
var mongoose = require('mongoose');
var CpuSchema = require("../schemas/cpu");
var Cpu = mongoose.model('Cpu', CpuSchema);
console.log(Cpu);
module.exports = Cpu;
one#demo ~/cloudimageshare-monitoring/project $ node /home/one/cloudimageshare-monitoring/project/app/data/models/cpu.js
{ [Function: model]
base:
{ connections: [ [Object] ],
plugins: [],
models: { Cpu: [Circular] },
modelSchemas: { Cpu: [Object] },
options: { pluralization: true } },
modelName: 'Cpu',
model: [Function: model],
db:
{ base:
{ connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: [Object] },
collections: { cpus: [Object] },
models: {},
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
_listening: false },
discriminators: undefined,
schema:
{ paths:
{ timeStamp: [Object],
avaiable: [Object],
status: [Object],
metrics: [Object],
_id: [Object],
__v: [Object] },
subpaths: {},
virtuals: { id: [Object] },
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
statics: {},
tree:
{ timeStamp: [Object],
avaiable: [Function: Boolean],
status: [Function: String],
metrics: [Object],
_id: [Object],
id: [Object],
__v: [Function: Number] },
_requiredpaths: undefined,
discriminatorMapping: undefined,
_indexedpaths: undefined,
options:
{ id: true,
noVirtualId: false,
_id: true,
noId: false,
read: null,
shardKey: null,
autoIndex: true,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
_events: {} },
options: undefined,
collection:
{ collection: null,
opts: { bufferCommands: true, capped: false },
name: 'cpus',
conn:
{ base: [Object],
collections: [Object],
models: {},
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
_listening: false },
queue: [ [Object] ],
buffer: true } }
Here on the model, I don't see a plugin() method.
The plugin method is defined on the Schema class and you can see it on your CpuSchema object.
On your Cpu model you can get it by calling
console.log(Cpu.schema.plugin)
From the mongoose source code on GitHub:
/**
* Registers a plugin for this schema.
*
* #param {Function} plugin callback
* #param {Object} opts
* #see plugins
* #api public
*/
Schema.prototype.plugin = function (fn, opts) {
fn(this, opts);
return this;
};
When you pass your plugin function to it it simply executes the function and passes the schema reference into it.