Error: Request failed with status code 400 (Axios, Graphql, Mongoose) - javascript

I made a server that handles webhook events from a service called Persona that verifies and collects identity information. Everytime I get a webhook call from Persona, I want to do 2 things: I want to take store some of the data from the webhook payload in my Mongoose database AND I want a notification to pop up in discord. If the information taken from the payload already exists in the mongoose db, then I want a notification on discord saying "user already exists". Otherwise, "new user".
My problem is that my axios request seems to be failing after getting the webhook call from Persona. This is my code:
require("dotenv").config();
const express = require("express");
const bodyParser = require('body-parser');
const axios = require("axios").default;
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const mongoose = require('mongoose');
const Event = require('./models/event');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST,GET,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
app.use('/graphql', graphqlHTTP({
schema: buildSchema(`
type Event {
_id: ID!
lastName: String!
firstName: String!
birthdate: String!
address: String!
}
input EventInput {
lastName: String!
firstName: String!
birthdate: String!
address: String!
}
type RootQuery {
events: [Event!]!
}
type RootMutation {
createEvent(eventInput: EventInput): Event
}
schema {
query: RootQuery
mutation: RootMutation
}
`),
rootValue: {
events: () => {
return Event.find()
.then(events => {
return events.map(event => {
return { ...event._doc, _id: event.id };
});
})
.catch(err => {
throw err;
});
},
createEvent: (args) => {
return Event.find({lastName: args.eventInput.lastName, firstName: args.eventInput.firstName, birthdate: args.eventInput.birthdate})
.then( event => {
if (event) {
const content = `User already exists: ${args.eventInput.firstName},${args.eventInput.firstName}. Birthdate:
${args.eventInput.birthdate}. address: ${args.eventInput.address}`;
axios
.post(process.env.DISCORD_WEBHOOK_URL, {
content: content,
})
.then((discordResponse) => {
console.log("Success!");
})
}
const content = `New User. address: ${args.eventInput.address}`;
axios
.post(process.env.DISCORD_WEBHOOK_URL, {
content: content,
})
.then((discordResponse) => {
console.log("Success!");
})
return event
.save()
.then(result => {
console.log(result);
return { ...result._doc, _id: result._doc._id.toString() };
})
.catch(err => {
console.log(err);
throw err;
});
})
}
},
graphiql: true
})
);
app.post("/", (req, res) => {
const nameOfEvent = req.body.data.attributes.name;
const firstName = req.body.data.attributes.payload.data.attributes.nameFirst;
const lastName = req.body.data.attributes.payload.data.attributes.nameLast;
const birthdate = req.body.data.attributes.payload.data.attributes.birthdate;
const address = req.body.data.attributes.payload.data.attributes.fields.address.value;
if (nameOfEvent == "inquiry.completed") {
axios({
url: 'http://localhost:3000/graphql',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
query: `
mutation {
createEvent(eventInput: {lastName: "${lastName}", firstName: "${firstName}", birthdate: "${birthdate}", address: "${address}}) {
lastName
firstName
}
}
`
}
})
.then(res => {
if (res.status !== 200 && res.status !== 201) {
throw new Error('Failed!');
}
return res.json();
})
.then(resData => {
console.log(resData);
})
.catch(err => {
console.log(err);
});
}})
;
app.use((error, req, res, next) => {
res.status(500)
res.send({ error: error })
console.error(error.stack)
next(error)
})
mongoose
.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}#cluster0.nrtrssh.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`
)
.then(() => {
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);
})
.catch(err => {
console.log(err);
});
This is my event schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const eventSchema = new Schema({
lastName: {
type: String,
required: true
},
firstName: {
type: String,
required: true
},
birthdate: {
type: String,
required: true
},
address: {
type: String,
required: true
}
});
module.exports = mongoose.model('Event', eventSchema);
This was I got in my terminal:
config: {
url: 'http://localhost:3000/graphql',
method: 'post',
data: '{"query":"\\n mutation {\\n createEvent(eventInput: {lastName: \\"SAMPLE\\", firstName: \\"ALEXANDER J\\", birthdate: \\"1977-07-17\\", address: \\"0x98433A5F2127b48Ae71f638e81f99b211e1F8ab6}) {\\n lastName\\n firstName\\n }\\n }\\n "}',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'User-Agent': 'axios/0.21.1',
'Content-Length': 266
},
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus]
},
request: <ref *1> ClientRequest {
_events: [Object: null prototype] {
abort: [Function (anonymous)],
aborted: [Function (anonymous)],
connect: [Function (anonymous)],
error: [Function (anonymous)],
socket: [Function (anonymous)],
timeout: [Function (anonymous)],
prefinish: [Function: requestOnPrefinish]
},
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: 'localhost',
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
parser: null,
_httpMessage: [Circular *1],
[Symbol(async_id_symbol)]: 262,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
},
_header: 'POST /graphql HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'User-Agent: axios/0.21.1\r\n' +
'Content-Length: 266\r\n' +
'Host: localhost:3000\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: noopPendingOutput],
agent: Agent {
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
maxTotalSockets: Infinity,
totalSocketCount: 1,
[Symbol(kCapture)]: false
},
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/graphql',
_ended: true,
res: IncomingMessage {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
socket: [Socket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 400,
statusMessage: 'Bad Request',
client: [Socket],
_consuming: false,
_dumped: false,
req: [Circular *1],
responseUrl: 'http://localhost:3000/graphql',
redirects: [],
[Symbol(kCapture)]: false,
[Symbol(RequestTimeout)]: undefined
},
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'localhost',
protocol: 'http:',
_redirectable: Writable {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_options: [Object],
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 266,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: [Circular *1],
_currentUrl: 'http://localhost:3000/graphql',
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype] {
accept: [Array],
'content-type': [Array],
'user-agent': [Array],
'content-length': [Array],
host: [Array]
}
},
response: {
status: 400,
statusText: 'Bad Request',
headers: {
'x-powered-by': 'Express',
'access-control-allow-origin': '*',
'access-control-allow-methods': 'POST,GET,OPTIONS',
'access-control-allow-headers': 'Content-Type, Authorization',
'content-type': 'application/json; charset=utf-8',
'content-length': '99',
etag: 'W/"63-STfhlTxeoEFxDo5wbBoiR3lYMrg"',
date: 'Sat, 21 Jan 2023 03:44:04 GMT',
connection: 'close'
},
config: {
url: 'http://localhost:3000/graphql',
method: 'post',
data: '{"query":"\\n mutation {\\n createEvent(eventInput: {lastName: \\"SAMPLE\\", firstName: \\"ALEXANDER J\\", birthdate: \\"1977-07-17\\", address: \\"0x98433A5F2127b48Ae71f638e81f99b211e1F8ab6}) {\\n lastName\\n firstName\\n }\\n }\\n "}',
headers: [Object],
transformRequest: [Array],
transformResponse: [Array],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus]
},
request: <ref *1> ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
_header: 'POST /graphql HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'User-Agent: axios/0.21.1\r\n' +
'Content-Length: 266\r\n' +
'Host: localhost:3000\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/graphql',
_ended: true,
res: [IncomingMessage],
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'localhost',
protocol: 'http:',
_redirectable: [Writable],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
},
data: { errors: [Array] }
},
isAxiosError: true,
toJSON: [Function: toJSON]
}

Related

Where is io.sockets.adapter.rooms in io of nodejs?

https://stackoverflow.com/a/6727354/462608
The short answer:
io.sockets.adapter.rooms
I analysed io:
The sockets output part from io as shown in that answer contains the following:
sockets:
{ manager: [Circular],
name: '',
sockets: { '210837319844898486': [Object] },
auth: false,
flags: { endpoint: '', exceptions: [] },
_events: { connection: [Function] } },
Where is the adapter? Where are the rooms?
What is the way to find out adapter and rooms from the output of io?
I think you are trying to get room before joining it. First You have to Join room and than you can get the rooms in io.sockets.adapter.rooms You can checkout this link to know rooms
let room_id = 111
io.sockets.on("connection", function (socket) {
// Everytime a client logs in, display a connected message
console.log("Server-Client Connected!");
socket.join("_room" + room_id);
socket.on('connected', function (data) {
});
console.log(io.sockets.adapter.rooms);
socket.on('qr_code_scan', function (room_id) {
io.sockets.in("_room" + room_id).emit("qr_code_scan", true);
});
});
Log of io.sockets.adapter.rooms
{bjYiUV5YZy54VedKAAAA: Room, _room111: Room}
app.js:55
_room111:Room {sockets: {…}, length: 1}
length:1
sockets:{-isBAZIB-Sm3jArgAAAB: true}
-isBAZIB-Sm3jArgAAAB:true
__proto__:Object
__proto__:Object
-isBAZIB-Sm3jArgAAAB:Room {sockets: {…}, length: 1}
length:1
sockets:{-isBAZIB-Sm3jArgAAAB: true}
-isBAZIB-Sm3jArgAAAB:true
__proto__:Object
__proto__:Object
__proto__:Object
For the current version of "socket.io": "^4.1.2",
io.sockets.adapter.rooms
is a Map like this:
Map(2) { 'hgdAp3ghn1RQZk3iAAAD' => Set(1) { 'hgdAp3ghn1RQZk3iAAAD'
}, 'test' => Set(1) { 'hgdAp3ghn1RQZk3iAAAD' } }
when a room already exist, in this case 'test'.
If you invoke it before a room has been created, then it'd be:
Map(1) { 'w2e2Vnav-zmf6pm4AAAD' => Set(1) { 'w2e2Vnav-zmf6pm4AAAD' } }
Thus the long answer is that it depends on the version you are using, and that for version 4.x the room will only be part of the map after an user has joined the room, not before.
I'm not sure what's up with that response. But I can confirm that when I run a basic socket.io example (codesandbox link) using socket.io#2.1.1 and console.log(io) I see the following in my terminal:
Server {
nsps: {
'/': Namespace {
name: '/',
server: [Circular],
sockets: [Object],
connected: [Object],
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter: [Adapter],
_events: [Object: null prototype],
_eventsCount: 1
}
},
parentNsps: Map {},
_path: '/socket.io',
_serveClient: true,
parser: {
protocol: 4,
types: [
'CONNECT',
'DISCONNECT',
'EVENT',
'ACK',
'ERROR',
'BINARY_EVENT',
'BINARY_ACK'
],
CONNECT: 0,
DISCONNECT: 1,
EVENT: 2,
ACK: 3,
ERROR: 4,
BINARY_EVENT: 5,
BINARY_ACK: 6,
Encoder: [Function: Encoder],
Decoder: [Function: Decoder]
},
encoder: Encoder {},
_adapter: [Function: Adapter],
_origins: '*:*',
sockets: Namespace {
name: '/',
server: [Circular],
sockets: { WFrro9MpS4d1nSouAAAA: [Socket] },
connected: { WFrro9MpS4d1nSouAAAA: [Socket] },
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter: Adapter {
nsp: [Circular],
rooms: [Object],
sids: [Object],
encoder: Encoder {}
},
_events: [Object: null prototype] { connection: [Array] },
_eventsCount: 1
},
eio: Server {
clients: { WFrro9MpS4d1nSouAAAA: [Socket] },
clientsCount: 1,
wsEngine: 'ws',
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: { threshold: 1024 },
httpCompression: { threshold: 1024 },
initialPacket: [ '0' ],
ws: WebSocketServer {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
options: [Object],
[Symbol(kCapture)]: false
},
_events: [Object: null prototype] { connection: [Function: bound ] },
_eventsCount: 1
},
httpServer: Server {
insecureHTTPParser: undefined,
_events: [Object: null prototype] {
connection: [Function: connectionListener],
close: [Function: bound ],
listening: [Function: bound ],
upgrade: [Function],
request: [Function]
},
_eventsCount: 5,
_maxListeners: undefined,
_connections: 2,
_handle: TCP {
reading: false,
onconnection: [Function: onconnection],
[Symbol(owner_symbol)]: [Circular]
},
_usingWorkers: false,
_workers: [],
_unref: false,
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
keepAliveTimeout: 5000,
maxHeadersCount: null,
headersTimeout: 60000,
_connectionKey: '6::::8080',
[Symbol(IncomingMessage)]: [Function: IncomingMessage],
[Symbol(ServerResponse)]: [Function: ServerResponse],
[Symbol(kCapture)]: false,
[Symbol(asyncId)]: 6
},
engine: Server {
clients: { WFrro9MpS4d1nSouAAAA: [Socket] },
clientsCount: 1,
wsEngine: 'ws',
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: { threshold: 1024 },
httpCompression: { threshold: 1024 },
initialPacket: [ '0' ],
ws: WebSocketServer {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
options: [Object],
[Symbol(kCapture)]: false
},
_events: [Object: null prototype] { connection: [Function: bound ] },
_eventsCount: 1
}
}
With the rooms in io.sockets.adapter.rooms.

Only one model in mongoose connection

I logged my connection and got this:
// Connect to Mongo
const promise = mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false }) // Adding new mongo url parser
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
console.log(promise);
Here' what is logged:
Promise { <pending> }
NativeConnection {
base: Mongoose {
connections: [ [Circular] ],
models: { user: Model { user } },
modelSchemas: { user: [Schema] },
options: { pluralization: true, [Symbol(mongoose:default)]: true },
_pluralize: [Function: pluralize],
Schema: [Function: Schema] {
reserved: [Object: null prototype],
Types: [Object],
ObjectId: [Function]
},
model: [Function],
plugins: [ [Array], [Array], [Array], [Array], [Array] ]
},
collections: {
users: NativeCollection {
collection: null,
Promise: [Function: Promise],
_closed: false,
opts: [Object],
name: 'users',
collectionName: 'users',
conn: [Circular],
queue: [],
buffer: true,
emitter: [EventEmitter]
}
},
models: { user: Model { user } },
config: { autoIndex: true, useCreateIndex: true, useFindAndModify: false },
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype] {
'0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99
},
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
plugins: [],
id: 0,
_listening: false,
_connectionString: 'Sorry, but cannot pass :)',
_connectionOptions: {
useNewUrlParser: true,
useUnifiedTopology: true,
promiseLibrary: [Function: Promise],
driverInfo: { name: 'Mongoose', version: '5.10.3' }
},
client: MongoClient {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
s: {
url: 'sorry, but cannot pass :)',
options: [Object],
promiseLibrary: [Function: Promise],
dbCache: Map {},
sessions: Set {},
writeConcern: undefined,
namespace: [MongoDBNamespace]
},
[Symbol(kCapture)]: false
},
'$initialConnection': Promise { <pending> },
then: [Function],
catch: [Function],
_events: [Object: null prototype] {
open: [Function: bound onceWrapper] { listener: [Function] }
},
_eventsCount: 1
The problem is that I have three models: post, user and message and because of that I cannot for example upload file with multer for message or post. Why is it happening? It cannot be a problem with my cluster, because I have second database on that cluster in other projects that works( which have implemented the same things, but with correct effect).
The only issue I think can be happening with this connection you might be passing a new schema and model that you will see in your object is with that schema. You don't get all schemas from the cloud, the schema being returned is the one that exists with this connection.

FindOne() working to return records, but cannot use MongoDB aggregate query (sum records for ids) for Node.js

I have managed to get the connection open to my DB, and to return single records (which took a long time for me to work out how to do!):
MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
MongoClient.connect('mongodb://localhost', function (err, client) {
if (err) throw err;
var db = client.db('payments');
db.collection('general').findOne({ "Physician_Profile_ID" : {$eq: 346085}}, {projection:
{'Physician_Profile_ID': 1,
'Total_Amount_of_Payment_USDollars': 1,
'Physician_First_Name': 1,
'Physician_Last_Name': 1}
}).then(function(doc) {
if(!doc)
throw new Error('No records found!');
console.log('Here is the record: ')
console.log(doc);
});
});
The issue i'm having is that I want to have another call that is able to aggregate the records on the specified physician_profile_id. I want to perform this shell query:
db.general.aggregate
([{$match:{Physician_Profile_ID: 346085}},
{$group:{_id: "$Physician_Profile_ID",
total:{$sum: "$Total_Amount_of_Payment_USDollars"}}}])
How can I translate this into Node.js's dialect? The syntax you use for the Mongo shell isn't translating over
I have tried the following:
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
//var o_id = new ObjectID("5b854c781c332b9558cece8d");
MongoClient.connect('mongodb://localhost', function (err, client) {
if (err) throw err;
var db = client.db('payments');
db.collection('general').aggregate({$match:{Physician_Profile_ID: 346085}},{$group:{_id: "$Physician_Profile_ID",
total:{$sum: "$Total_Amount_of_Payment_USDollars"}}}).then(function(doc) {
if(!doc)
throw new Error('No records found!');
console.log('Here is the bastard record: ')
console.log(doc);
});
});
It's throwing this error:
throw err;
^
TypeError: db.collection(...).aggregate(...).then is not a function
at /Users/Badger/mongodb_connect.js:35:67
at result (/Users/Badger/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/Users/Badger/node_modules/mongodb/lib/utils.js:406:9)
at err (/Users/Badger/node_modules/mongodb/lib/operations/mongo_client_ops.js:286:5)
at connectCallback (/Users/Badger/node_modules/mongodb/lib/operations/mongo_client_ops.js:241:5)
at process.nextTick (/Users/Badger/node_modules/mongodb/lib/operations/mongo_client_ops.js:463:7)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)
Please can someone help me out, i've been looking for a few hours now and have searched on the site but aren't getting any luck. It's not making sense to me that the error is stating that aggregate isn't a function, when findOne() is. Unless aggregate has to be nested inside of Find(), but this isn't working for me either
Many thanks
Update:
Running this code:
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
//var o_id = new ObjectID("5b854c781c332b9558cece8d");
MongoClient.connect('mongodb://localhost', function (err, client) {
if (err) throw err;
var db = client.db('payments');
db.collection('general').aggregate([{$match:{Physician_Profile_ID: 346085}},{$group:{_id: "$Physician_Profile_ID",
total:{$sum: "$Total_Amount_of_Payment_USDollars"}}}], function(err,doc) {
if(err)
throw new Error('No records found!');
console.log('Here is the bastard record: ')
console.log(doc);
});
});
Is returning output that is not expected:
AggregationCursor {
pool: null,
server: null,
disconnectHandler:
Store {
s: { storedOps: [], storeOptions: [Object], topology: [Object] },
length: [Getter] },
bson: BSON {},
ns: 'payments.general',
cmd:
{ aggregate: 'general',
pipeline: [ [Object], [Object] ],
cursor: {} },
options:
{ readPreference: ReadPreference { mode: 'primary', tags: undefined },
cursor: {},
promiseLibrary: [Function: Promise],
cursorFactory: { [Function: AggregationCursor] super_: [Object], INIT: 0, OPEN: 1, CLOSED: 2 },
disconnectHandler: Store { s: [Object], length: [Getter] },
topology:
Server {
domain: null,
_events: [Object],
_eventsCount: 25,
_maxListeners: Infinity,
clientInfo: [Object],
s: [Object] } },
topology:
Server {
domain: null,
_events:
{ serverOpening: [Function],
serverDescriptionChanged: [Function],
serverHeartbeatStarted: [Function],
serverHeartbeatSucceeded: [Function],
serverHeartbeatFailed: [Function],
serverClosed: [Function],
topologyOpening: [Function],
topologyClosed: [Function],
topologyDescriptionChanged: [Function],
commandStarted: [Function],
commandSucceeded: [Function],
commandFailed: [Function],
joined: [Function],
left: [Function],
ping: [Function],
ha: [Function],
authenticated: [Function],
error: [Array],
timeout: [Array],
close: [Array],
parseError: [Array],
open: [Array],
fullsetup: [Array],
all: [Array],
reconnect: [Array] },
_eventsCount: 25,
_maxListeners: Infinity,
clientInfo:
{ driver: [Object],
os: [Object],
platform: 'Node.js v8.12.0, LE' },
s:
{ coreTopology: [Object],
sCapabilities: [Object],
clonedOptions: [Object],
reconnect: true,
emitError: true,
poolSize: 5,
storeOptions: [Object],
store: [Object],
host: 'localhost',
port: 27017,
options: [Object],
sessionPool: [Object],
sessions: [],
promiseLibrary: [Function: Promise] } },
cursorState:
{ cursorId: null,
cmd: { aggregate: 'general', pipeline: [Array], cursor: {} },
documents: [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: 0,
skip: 0,
batchSize: 1000,
currentLimit: 0,
transforms: undefined,
reconnect: true },
logger: Logger { className: 'Cursor' },
_readableState:
ReadableState {
objectMode: true,
highWaterMark: 16,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s:
{ maxTimeMS: null,
state: 0,
streamOptions: {},
bson: BSON {},
ns: 'payments.general',
cmd: { aggregate: 'general', pipeline: [Array], cursor: {} },
options:
{ readPreference: [Object],
cursor: {},
promiseLibrary: [Function: Promise],
cursorFactory: [Object],
disconnectHandler: [Object],
topology: [Object] },
topology:
Server {
domain: null,
_events: [Object],
_eventsCount: 25,
_maxListeners: Infinity,
clientInfo: [Object],
s: [Object] },
topologyOptions:
{ host: 'localhost',
port: 27017,
disconnectHandler: [Object],
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
monitorCommands: false,
socketOptions: {},
socketTimeout: 360000,
connectionTimeout: 30000,
promiseLibrary: [Function: Promise],
clientInfo: [Object],
read_preference_tags: null,
readPreference: [Object],
dbName: 'admin',
servers: [Array],
server_options: [Object],
db_options: [Object],
rs_options: [Object],
mongos_options: [Object],
socketTimeoutMS: 360000,
connectTimeoutMS: 30000,
bson: BSON {} },
promiseLibrary: [Function: Promise],
session: undefined },
sortValue: undefined }
You have to use db.collection('general').aggregate([{}])
when you do .aggregate it will return you a cursor, and with the cursor you can loop cursor.each but what you want to do most of the cases is to transform it into an array.
MongoClient.connect('mongodb://localhost', function (err, client) {
if (err) throw err;
var db = client.db('payments');
db.collection('general').aggregate([{$match:{Physician_Profile_ID: 346085}},{$group:{_id: "$Physician_Profile_ID",
total:{$sum: "$Total_Amount_of_Payment_USDollars"}}}]).toArray(function(err,doc) {
if(err)
throw new Error('No records found!');
console.log('Here is the bastard record: ')
console.log(doc);
});
});

Mongoose model on MongoDB collection fetching no data

I am using MongoDB version 3.4.7, Mongoose 4.11.13 driver for creating Model on MongoDB collection and creating AggregationCursor to process each document individually to populate some JS variables. But it seems the Mongoose model is not bringing any data into the cursor from MongoDB collection.
I tried aggregate operation with mongodb driver previously but it doesn't work
(check here) and opted for mongoose instead. But it seems there is some problem here as well or I am missing anything here? Updated my AggregationCursor for better understanding. Any quick help will be greatly appreciated.
Node version 3.10.10, ExpressJS 3.2.6
My Node.JS code is as follows:
app.get('/reviews',function(req,res)
{
res.render("chart");
var obj = {};
var mongoose = require('mongoose');
var assert = require('assert');
var schema = new mongoose.Schema({
seller_id: String,
product_id: String,
product_desc: String,
reviews: {
total_review_count: Number,
five_star_count: Number,
four_star_count: Number,
three_star_count: Number,
two_star_count: Number,
one_star_count: Number
}
});
//specify the name of MongoDB collection as third argument of model
var ProductReview = mongoose.model('ProductReview', schema, 'product_reviews'); //<-- here
//review count placeholders
var totalReviewCounts = [], FiveStarCounts = [], FourStarCounts = [], ThreeStarCounts = [],
TwoStarCounts = [], OneStarCounts = [];
mongoose.connect('mongodb://localhost:27017/sellerrank', function() {
var cursor = ProductReview.aggregate([
{
$match: {
seller_id: { $exists: true, $nin: [null] }
}
}
])
.allowDiskUse(true)
.group({ _id: { seller_id: '$seller_id'},
total_review_count: {
$sum: {
$cond : [
{ $eq : ["$reviews.total_review_count", null] },
0,
"$reviews.total_review_count"
]
}
},
five_star_count: {
$sum: {
$cond : [
{ $eq : ["$reviews.five_star_count", null] },
0,
"$reviews.five_star_count"
]
}
},
four_star_count: {
$sum: {
$cond : [
{ $eq : ["$reviews.four_star_count", null] },
0,
"$reviews.four_star_count"
]
}
},
three_star_count: {
$sum: {
$cond : [
{ $eq : ["$reviews.three_star_count", null] },
0,
"$reviews.three_star_count"
]
}
},
two_star_count: {
$sum: {
$cond : [
{ $eq : ["$reviews.two_star_count", null] },
0,
"$reviews.two_star_count"
]
}
},
one_star_count: {
$sum: {
$cond : [
{ $eq : ["$reviews.one_star_count", null] },
0,
"$reviews.one_star_count"
]
}
}
})
.cursor({ batchSize: 100})
.exec();
console.log(cursor); **//<-- printing the cursor here**
cursor.each(function(err,doc){
assert.equal(null,err);
console.log(doc);
if(doc!=null){
var seller_id = doc['_id'];
var totalReviews = doc['total_review_count'];
var fiveStarReviews = doc['five_star_count'];
var fourStarReviews = doc['four_star_count'];
var threeStarReviews = doc['three_star_count'];
var twoStarReviews = doc['two_star_count'];
var oneStarReviews = doc['one_star_count'];
totalReviewCounts.push({"value" : totalReviews});
FiveStarCounts.push({"value" : fiveStarReviews});
FourStarCounts.push({"value" : fourStarReviews});
ThreeStarCounts.push({"value" : threeStarReviews});
TwoStarCounts.push({"value" : twoStarReviews});
OneStarCounts.push({"value" : oneStarReviews});
}
});
});
var dataset = [
{
"seriesname" : "Total positive reviews",
"data" : totalReviewCounts
},
{
"seriesname" : "5 star reviews",
"data": FiveStarCounts
},
{
"seriesname" : "4 star reviews",
"data" : FourStarCounts
},
{
"seriesname" : "3 star reviews",
"data": ThreeStarCounts
},
{
"seriesname" : "2 star reviews",
"data" : TwoStarCounts
},
{
"seriesname" : "1 star reviews",
"data": OneStarCounts
}
];
console.log(dataset); //<-- **dataset is printed here**
var response = {
"dataset" : dataset
};
obj['dataset'] = dataset;
console.log(obj);
res.send(obj);
mongoose.disconnect();
});
When I am running the above Node.JS Express code, I am getting the output of console.log(dataset) as
[ { seriesname: 'Total positive reviews', data: [] },
{ seriesname: '5 star reviews', data: [] },
{ seriesname: '4 star reviews', data: [] },
{ seriesname: '3 star reviews', data: [] },
{ seriesname: '2 star reviews', data: [] },
{ seriesname: '1 star reviews', data: [] } ]
There is data in my local MongoDB collection 'product_reviews' but there is no data fetched into the cursor.
My Aggregation cursor as shown in console.log(cursor) is as follows:
AggregationCursor {
pool: null,
server: null,
disconnectHandler:
Store {
s: { storedOps: [], storeOptions: [Object], topology: [Object] },
length: [Getter] },
bson: BSON {},
ns: 'sellerrank.product_reviews',
cmd:
{ aggregate: 'product_reviews',
pipeline: [ [Object], [Object] ],
allowDiskUse: true,
cursor: { batchSize: 100 } },
options:
{ allowDiskUse: true,
cursor: { batchSize: 100 },
promiseLibrary: [Function: Promise],
cursorFactory: { [Function: AggregationCursor] super_: [Object], define: [Object], INIT: 0, OPEN: 1, CLOSED: 2 },
disconnectHandler: Store { s: [Object], length: [Getter] } },
topology:
Server {
domain: null,
_events:
{ reconnect: [Function: reconnectHandler],
reconnectFailed: [Function: reconnectFailedHandler],
serverDescriptionChanged: [Function],
serverHeartbeatStarted: [Function],
serverHeartbeatSucceeded: [Function],
serverHeartbeatFailed: [Function],
serverOpening: [Function],
serverClosed: [Function],
topologyOpening: [Function],
topologyClosed: [Function],
topologyDescriptionChanged: [Function],
attemptReconnect: [Function],
monitoring: [Function],
timeout: [Function],
error: [Object],
close: [Function],
destroy: [Function: destroyHandler] },
_eventsCount: 17,
_maxListeners: undefined,
id: 0,
s:
{ options: [Object],
logger: [Object],
Cursor: [Object],
bson: BSON {},
pool: [Object],
disconnectHandler: [Object],
monitoring: true,
inTopology: false,
monitoringInterval: 5000,
topologyId: -1,
serverDescription: [Object],
topologyDescription: [Object] },
ismaster:
{ ismaster: true,
maxBsonObjectSize: 16777216,
maxMessageSizeBytes: 48000000,
maxWriteBatchSize: 1000,
localTime: 2017-10-02T14:21:27.032Z,
maxWireVersion: 5,
minWireVersion: 0,
readOnly: false,
ok: 1 },
lastIsMasterMS: 12,
monitoringProcessId:
Timeout {
_called: false,
_idleTimeout: 5000,
_idlePrev: [Object],
_idleNext: [Object],
_idleStart: 5621,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null },
initalConnect: false,
wireProtocolHandler: WireProtocol { legacyWireProtocol: WireProtocol {} },
_type: 'server',
clientInfo:
{ driver: [Object],
os: [Object],
platform: 'Node.js v6.11.3, LE, mongodb-core: 2.1.15' },
lastUpdateTime: 0,
lastWriteDate: 0,
staleness: 0 },
cursorState:
{ cursorId: null,
cmd:
{ aggregate: 'product_reviews',
pipeline: [Object],
allowDiskUse: true,
cursor: [Object] },
documents: [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: 0,
skip: 0,
batchSize: 100,
currentLimit: 0,
transforms: undefined },
logger: Logger { className: 'Cursor' },
_readableState:
ReadableState {
objectMode: true,
highWaterMark: 16,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s:
{ maxTimeMS: null,
state: 0,
streamOptions: {},
bson: BSON {},
ns: 'sellerrank.product_reviews',
cmd:
{ aggregate: 'product_reviews',
pipeline: [Object],
allowDiskUse: true,
cursor: [Object] },
options:
{ allowDiskUse: true,
cursor: [Object],
promiseLibrary: [Function: Promise],
cursorFactory: [Object],
disconnectHandler: [Object] },
topology:
Server {
domain: null,
_events: [Object],
_eventsCount: 17,
_maxListeners: undefined,
id: 0,
s: [Object],
ismaster: [Object],
lastIsMasterMS: 12,
monitoringProcessId: [Object],
initalConnect: false,
wireProtocolHandler: [Object],
_type: 'server',
clientInfo: [Object],
lastUpdateTime: 0,
lastWriteDate: 0,
staleness: 0 },
topologyOptions:
{ host: 'localhost',
port: 27017,
disconnectHandler: [Object],
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
socketOptions: {},
auto_reconnect: true,
clientInfo: [Object],
forceServerObjectId: false,
w: 1,
promiseLibrary: [Function: Promise],
bson: BSON {} },
promiseLibrary: [Function: Promise] },
sortValue: undefined,
eachAsync: [Function] }

how to connect mongoDB to server?

I am trying to get data from mongoDB while connecting to server .i inserted one value in mongoDB like this
> use abc
switched to db abc
> db.ac.insert({name:"naveen"})
WriteResult({ "nInserted" : 1 })
> show collections
ac
system.indexes
And try to get that value like this
var express=require('express');
var app =express();
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
app.get('/',function(req,res){
console.log("Connected to server.");
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var collection = db.collection('ac');
console.log(collection)
console.log("Connected correctly to server.");
db.close();
});
})
var url = 'mongodb://localhost:27017/abc';
app.listen(3000,function(){
console.log('server is runninngn')
})
I am getting this error why ?
MacBook-Pro:expressjs naveenkumar$ node index.js
server is runninngn
Connected to server.
{ s:
{ pkFactory:
{ [Function: ObjectID]
index: 462263,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString],
isValid: [Function: isValid],
ObjectID: [Circular],
ObjectId: [Circular] },
db:
{ domain: null,
_events: {},
_maxListeners: 10,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
options: [Getter],
native_parser: [Getter],
slaveOk: [Getter],
writeConcern: [Getter] },
topology:
{ domain: null,
_events: [Object],
_maxListeners: 10,
s: [Object],
bson: [Getter],
isMasterDoc: [Getter],
poolSize: [Getter],
autoReconnect: [Getter],
host: [Getter],
port: [Getter],
emitOpen: false,
connectTimeoutMS: 30000,
socketTimeoutMS: 0 },
dbName: 'abc',
options: { promiseLibrary: [Object], readConcern: undefined },
namespace: 'abc.ac',
readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
raw: undefined,
slaveOk: true,
serializeFunctions: undefined,
internalHint: null,
collectionHint: null,
name: 'ac',
promiseLibrary:
{ [Function: lib$es6$promise$promise$$Promise]
all: [Function: lib$es6$promise$promise$all$$all],
race: [Function: lib$es6$promise$promise$race$$race],
resolve: [Function: lib$es6$promise$promise$resolve$$resolve],
reject: [Function: lib$es6$promise$promise$reject$$reject] },
readConcern: undefined } }
Connected correctly to server.
Connected to server.
{ s:
{ pkFactory:
{ [Function: ObjectID]
index: 462263,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString],
isValid: [Function: isValid],
ObjectID: [Circular],
ObjectId: [Circular] },
db:
{ domain: null,
_events: {},
_maxListeners: 10,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
options: [Getter],
native_parser: [Getter],
slaveOk: [Getter],
writeConcern: [Getter] },
topology:
{ domain: null,
_events: [Object],
_maxListeners: 10,
s: [Object],
bson: [Getter],
isMasterDoc: [Getter],
poolSize: [Getter],
autoReconnect: [Getter],
host: [Getter],
port: [Getter],
emitOpen: false,
connectTimeoutMS: 30000,
socketTimeoutMS: 0 },
dbName: 'abc',
options: { promiseLibrary: [Object], readConcern: undefined },
namespace: 'abc.ac',
readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
raw: undefined,
slaveOk: true,
serializeFunctions: undefined,
internalHint: null,
collectionHint: null,
name: 'ac',
promiseLibrary:
{ [Function: lib$es6$promise$promise$$Promise]
all: [Function: lib$es6$promise$promise$all$$all],
race: [Function: lib$es6$promise$promise$race$$race],
resolve: [Function: lib$es6$promise$promise$resolve$$resolve],
reject: [Function: lib$es6$promise$promise$reject$$reject] },
readConcern: undefined } }
**Connected correctly to server.**
Please why it is giving error
If you want to display all the documents in the given collection the code should be like this :
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var collection = db.collection('ac').find({},function(err,doc){
if(err)
throw err;
else{
console.log(doc);
}
});
console.log("Connected correctly to server.");
db.close();
});
this will print all the documents of collection ac. Otherwise if you directly try to print collection all the metadata will also get printed.

Categories