passing data with nodejs/mongodb to the frontend (undefined on the browser) - javascript

I would like to get data from a mongodb database and pass it to the frond.
I wrote this function it works in the console i get an array that contains some elements but on the browser i get undefined. I use nodejs(express, ejs) and mongodb.
getAllOrders: function(companyName) {
client.connect().then((client)=>{
let db = client.db('data')
db.collection('order').find({companyName:companyName}).toArray(function(err, result) {
if (err) throw err
console.log(JSON.stringify(result.map(a => a.orderIdem))) // output all orderIdems
});
});
}
//App.js
app.get('/', async function(req, res) {
let orderItem = await db.getAllOrders("SuperTrader");
console.log(orderItem); // here i get a response
res.render('index', {
orderItem: orderItem,
});
});
// frontend
//index.ejs
<h2>Variable</h2>
<p><%= orderItem %></p>
//orderItem[0] also doesn't work

there is nothing you are returning from the function. try this:
getAllOrders: async function(companyName) {
return client.connect().then((client)=>{
let db = client.db('data')
return await db.collection('order').find({companyName:companyName});
});
}

Related

Mongoose - find() not returning anything when no parameters are passed, but returns data when parameters are passed

I have the following model in teamMembers.js:
const { Schema, model } = require('mongoose');
const teamMembersSchema = new Schema({
uid: String,
name: String,
hours: Number
})
const TeamMembers = model('teamMembers', TeamMembersSchema);
module.exports = TeamMembers;
I've created the following endpoints in teamMemberRoute.js:
const TeamMembers = require('./models/teamMembers');
module.exports = (app) => {
app.get('/api/pods/teamMembers/:uid', async (req, res) => {
let teamMember = await TeamMembers.find( {'uid': req.params.uid } );
return res.status(200).send(teamMember);
});
app.get('/api/pods/teamMembers', async (req, res) => {
let teamMembers = await TeamMembers.find();
return res.status(200).send(teamMembers);
});
}
The first endpoint (/api/pods/teamMembers/:uid) works just fine - when I pass a uid it returns documents specific to that uid in the TeamMember collection.
The second endpoint should return all documents from the TeamMember collection since no parameters are passed. However, when the request is executed, only [] is returned. We know for a fact that documents exist in the TeamMember collection, since the first endpoint returns data from that collection based on the uid parameter that is passed.
I'm stumped on this. Any ideas? I don't think there is anything wrong with my model since I am able to execute the first endpoint with no issues.
Express executes code from top to button, and that is the reason for this issue. It will match your first endpoint and assume that uid is null. Just change the order of defined endpoints, like this:
module.exports = (app) => {
app.get('/api/pods/teamMembers', async (req, res) => {
let teamMembers = await TeamMembers.find();
return res.status(200).send(teamMembers);
});
app.get('/api/pods/teamMembers/:uid', async (req, res) => {
let teamMember = await TeamMembers.find( {'uid': req.params.uid } );
return res.status(200).send(teamMember);
});
}

Why is the 'find' query returning a Query object in MongoDB?

I have already looked at many answers here at SO, but none of them solved my problem.
I'm currently creating a project using node, express, and mongodb. I started with a seeder file to put some data into mongodb:
var products = [
new ProductModel(data),
new ProductModel(data),
new ProductModel(data),
new ProductModel(data),
];
// Connecting to mongodb using mongoose.
mongoose.connect(config.mongodb.dsn, { useNewUrlParser: true })
.then(() => {
console.log("Successfully connected to MongoDB through Mongoose.");
})
.catch((error) => {
console.log("Error when connecting to MongoDB through Mongoose: " + error);
});
// Saving product instances to the product document in mongodb
var done = 0;
for (var i = 0; i < products.length; i++) {
products[i].save((error, result) => {
done++;
if (done == products.length) {
// Disconnecting...
console.log("All products have been loaded into mongodb. Disconnecting now...");
mongoose.disconnect();
}
});
}
Then, I am trying to retrieve this data in my routes file, using:
router.get('/', function(req, res, next) {
const products = ProductModel.find();
console.log(products);
res.render('shop/index', { products });
});
However, I am console.logging the result on my terminal, and the only result that comes up is a query object that looks like this:
Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map {}, _posts: Map {} },
_executionCount: 0,
mongooseCollection:
NativeCollection {
(...)
_traceFunction: undefined,
'$useProjection': true }
I needed to retrieve the products in my db. What am I doing wrong?
Everything is executed as it should. find() returns a Query object as per the docs: https://mongoosejs.com/docs/api.html#model_Model.find
If you want it to return the value, just turn it to an await and call exec to get a promise:
router.get('/', async function(req, res, next) {
const products = await ProductModel.find().exec();
console.log(products);
res.render('shop/index', { products });
});

After Summing Amounts Getting 'Converting circular structure to JSON' Error in MongoDB [duplicate]

I have some code that pulls all documents from a collection and puts it onto a webpage. a simplified version looks like this:
var mongodb = require("mongodb"),
express = require("express"),
mongoServer = new mongodb.Server('localhost', 27017),
dbConnector = new mongodb.Db('systemMonitor', mongoServer),
db;
var app = new express();
app.get('/drives', function(req, res) {
db.collection('driveInfo', function(err, collection) {
if (err) throw err;
collection.find({}, function(err, documents) {
res.send(documents);
});
});
});
dbConnector.open(function(err, opendb) {
if (err) throw err;
db = opendb;
app.listen(80);
});
I have a driveInfo collection which contains a long list of documents. Each document contains nested objects. What I would like to do, is whenever someone visits /drives in their browser, to print the entire collection as a json object so that I can grab everything with jquery later (beginnings of an api)
However, I get an error saying "TypeError: Converting circular structure to JSON". The error on the page points to this line of code:
collection.find({}, function(err, documents) {
res.send(documents);
});
I'm unsure what the problem is, or where the self-reference is. Am I not querying the collection properly?
Not sure what version of the API you are using, but i think that your syntax might be wrong looking at the API spec:
http://docs.mongodb.org/manual/reference/method/db.collection.find/
This is the declaration:
db.collection.find(<criteria>, <projection>)
And you are definitely misusing the projection parameter. Passing a callback like you are doing seems to return the db object in the result, which is causing the circular error during JSON serialization in express.
The correct code for the find all operation should be something like:
collection.find({}).toArray(function(error, documents) {
if (err) throw error;
res.send(documents);
});
In my case I was getting the error because I was querying(using mongoose find method) without doing an await. Please see below
Query that gave the error (as I haven't executed this query using await) :
const tours = Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
Error that I got on postman due to this :
"message": "Converting circular structure to JSON\n --> starting at object with constructor 'NativeTopology'\n | property 's' -> object with constructor 'Object'\n | property 'sessionPool' -> object with constructor 'ServerSessionPool'\n --- property 'topology' closes the circle"
How I got rid of the above error (added await) :
const tours = await Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
callback option is from Mongoose not from MongoDB see docs.
// Mongoose Docs : callback option
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
// Example
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
Look that the response is into callback that in your case it would be
YourModel.find({}, function(err, documents) {
if(err) return done(err);
res.send(documents); // <-- here
});
// <-- not here
In Mongo there is a cursor method to access the documents next() see docs :
var myCursor = db.bios.find( );
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
if (myDocument) {
var myName = myDocument.name;
print (tojson(myName));
}
You can find CRUD operations in mongo docs at manual/crud. In Query Documents you will see db.inventory.find( {} ) : To select all documents in the collection, pass an empty document as the query filter parameter to the find method.
Async/Await function solution : Mongo Docs
app.get( '/api/users' , async (req,res)=>{
const getUsers = await NewUser.find({});
res.json( getUsers );
})
< callback > solution : Mongoose Docs.
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
const res1 = await db.collection("some-db").find()
Here, res1 will contain a "cursor" which has a circular structure, hence the given error is thrown.
Try adding const res2 = await res1.toArray() to the code.
Here, res2 will now contain an array of documents, pointed by cursor res1, which is the documents you were querying for.

mongodb nodejs - converting circular structure

I have some code that pulls all documents from a collection and puts it onto a webpage. a simplified version looks like this:
var mongodb = require("mongodb"),
express = require("express"),
mongoServer = new mongodb.Server('localhost', 27017),
dbConnector = new mongodb.Db('systemMonitor', mongoServer),
db;
var app = new express();
app.get('/drives', function(req, res) {
db.collection('driveInfo', function(err, collection) {
if (err) throw err;
collection.find({}, function(err, documents) {
res.send(documents);
});
});
});
dbConnector.open(function(err, opendb) {
if (err) throw err;
db = opendb;
app.listen(80);
});
I have a driveInfo collection which contains a long list of documents. Each document contains nested objects. What I would like to do, is whenever someone visits /drives in their browser, to print the entire collection as a json object so that I can grab everything with jquery later (beginnings of an api)
However, I get an error saying "TypeError: Converting circular structure to JSON". The error on the page points to this line of code:
collection.find({}, function(err, documents) {
res.send(documents);
});
I'm unsure what the problem is, or where the self-reference is. Am I not querying the collection properly?
Not sure what version of the API you are using, but i think that your syntax might be wrong looking at the API spec:
http://docs.mongodb.org/manual/reference/method/db.collection.find/
This is the declaration:
db.collection.find(<criteria>, <projection>)
And you are definitely misusing the projection parameter. Passing a callback like you are doing seems to return the db object in the result, which is causing the circular error during JSON serialization in express.
The correct code for the find all operation should be something like:
collection.find({}).toArray(function(error, documents) {
if (err) throw error;
res.send(documents);
});
In my case I was getting the error because I was querying(using mongoose find method) without doing an await. Please see below
Query that gave the error (as I haven't executed this query using await) :
const tours = Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
Error that I got on postman due to this :
"message": "Converting circular structure to JSON\n --> starting at object with constructor 'NativeTopology'\n | property 's' -> object with constructor 'Object'\n | property 'sessionPool' -> object with constructor 'ServerSessionPool'\n --- property 'topology' closes the circle"
How I got rid of the above error (added await) :
const tours = await Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
callback option is from Mongoose not from MongoDB see docs.
// Mongoose Docs : callback option
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
// Example
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
Look that the response is into callback that in your case it would be
YourModel.find({}, function(err, documents) {
if(err) return done(err);
res.send(documents); // <-- here
});
// <-- not here
In Mongo there is a cursor method to access the documents next() see docs :
var myCursor = db.bios.find( );
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
if (myDocument) {
var myName = myDocument.name;
print (tojson(myName));
}
You can find CRUD operations in mongo docs at manual/crud. In Query Documents you will see db.inventory.find( {} ) : To select all documents in the collection, pass an empty document as the query filter parameter to the find method.
Async/Await function solution : Mongo Docs
app.get( '/api/users' , async (req,res)=>{
const getUsers = await NewUser.find({});
res.json( getUsers );
})
< callback > solution : Mongoose Docs.
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
const res1 = await db.collection("some-db").find()
Here, res1 will contain a "cursor" which has a circular structure, hence the given error is thrown.
Try adding const res2 = await res1.toArray() to the code.
Here, res2 will now contain an array of documents, pointed by cursor res1, which is the documents you were querying for.

Get the _id of inserted document in Mongo database in NodeJS

I use NodeJS to insert documents in MongoDB. Using collection.insert I can insert a document into database like in this code:
// ...
collection.insert(objectToInsert, function(err){
if (err) return;
// Object inserted successfully.
var objectId; // = ???
});
// ...
How can I get the _id of inserted object?
Is there any way to get the _id without getting latest object inserted _id?
Supposing that in same time a lot of people access the database, I can't be sure that the latest id is the id of object inserted.
A shorter way than using second parameter for the callback of collection.insert would be using objectToInsert._id that returns the _id (inside of the callback function, supposing it was a successful operation).
The Mongo driver for NodeJS appends the _id field to the original object reference, so it's easy to get the inserted id using the original object:
collection.insert(objectToInsert, function(err){
if (err) return;
// Object inserted successfully.
var objectId = objectToInsert._id; // this will return the id of object inserted
});
There is a second parameter for the callback for collection.insert that will return the doc or docs inserted, which should have _ids.
Try:
collection.insert(objectToInsert, function(err,docsInserted){
console.log(docsInserted);
});
and check the console to see what I mean.
As ktretyak said, to get inserted document's ID best way is to use insertedId property on result object. In my case result._id didn't work so I had to use following:
db.collection("collection-name")
.insertOne(document)
.then(result => {
console.log(result.insertedId);
})
.catch(err => {
// handle error
});
It's the same thing if you use callbacks.
I actually did a console.log() for the second parameter in the callback function for insert. There is actually a lot of information returned apart from the inserted object itself. So the code below explains how you can access it's id.
collection.insert(objToInsert, function (err, result){
if(err)console.log(err);
else {
console.log(result["ops"][0]["_id"]);
// The above statement will output the id of the
// inserted object
}
});
if you want to take "_id" use simpley
result.insertedId.toString()
// toString will convert from hex
Mongo sends the complete document as a callbackobject so you can simply get it from there only.
for example
collection.save(function(err,room){
var newRoomId = room._id;
});
You could use async functions to get _id field automatically without manipulating data object:
async function save() {
const data = {
name: "John"
}
await db.collection('users').insertOne(data)
return data
}
Returns (data object):
{
    _id: '5dbff150b407cc129ab571ca',
    name: 'John',
}
Now you can use insertOne method and in promise's result.insertedId
#JSideris, sample code for getting insertedId.
db.collection(COLLECTION).insertOne(data, (err, result) => {
if (err)
return err;
else
return result.insertedId;
});
Similar to other responses, you can grab the variable using async await, es6+ features.
const insertData = async (data) => {
const { ops } = await db.collection('collection').insertOne(data)
console.log(ops[0]._id)
}
Another way to do it in async function :
const express = require('express')
const path = require('path')
const db = require(path.join(__dirname, '../database/config')).db;
const router = express.Router()
// Create.R.U.D
router.post('/new-order', async function (req, res, next) {
// security check
if (Object.keys(req.body).length === 0) {
res.status(404).send({
msg: "Error",
code: 404
});
return;
}
try {
// operations
let orderNumber = await db.collection('orders').countDocuments()
let number = orderNumber + 1
let order = {
number: number,
customer: req.body.customer,
products: req.body.products,
totalProducts: req.body.totalProducts,
totalCost: req.body.totalCost,
type: req.body.type,
time: req.body.time,
date: req.body.date,
timeStamp: Date.now(),
}
if (req.body.direction) {
order.direction = req.body.direction
}
if (req.body.specialRequests) {
order.specialRequests = req.body.specialRequests
}
// Here newOrder will store some informations in result of this process.
// You can find the inserted id and some informations there too.
let newOrder = await db.collection('orders').insertOne({...order})
if (newOrder) {
// MARK: Server response
res.status(201).send({
msg: `Order N°${number} created : id[${newOrder.insertedId}]`,
code: 201
});
} else {
// MARK: Server response
res.status(404).send({
msg: `Order N°${number} not created`,
code: 404
});
}
} catch (e) {
print(e)
return
}
})
// C.Read.U.D
// C.R.Update.D
// C.R.U.Delete
module.exports = router;

Categories