Mongoose populate find array of ObjectId - node.js

I'm trying to populate an array of ObjectIds when doing a find with Mongoose.
Here is the models/comment.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// set up a mongoose model
var CommentSchema = new Schema({
comment: {
type: String,
unique: false,
required: true
},
date: {
type: Date,
default: new Date()
},
});
module.exports = mongoose.model('Comment', CommentSchema);
Here is the models/question.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Comment = require('./comment');
// set up a mongoose model
var QuestionSchema = new Schema({
title: {
type: String,
unique: false,
required: true
},
question: {
type: String,
required: true
},
plus: {
type: Number,
required: true,
default: 0
},
minus: {
type: Number,
required: true,
default: 0
},
date: {
type: Date,
default: new Date()
},
comments:[
{type: Schema.Types.ObjectId, ref: 'Comment'}
]
});
module.exports = mongoose.model('Question', QuestionSchema);
Here is index.js
var express = require('express');
var app = express();
var request = require('request-promise');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var config = require('./config/database'); // get db config file
var User = require('./models/user'); // get the mongoose model
var Room = require('./models/room'); // get the mongoose model
var Comment = require('./models/comment'); // get the mongoose model
var Question = require('./models/question'); // get the mongoose model
var port = process.env.PORT || 5000;
var jwt = require('jwt-simple');
var http = require('http');
var io = require('socket.io');
var server = http.createServer(app);
var io = io.listen(server);
// get our request parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// log to console
app.use(morgan('dev'));
// Use the passport package in our application
app.use(passport.initialize());
// Set the port
app.set('port', port);
//App files located in /public
app.use(express.static(__dirname + '/public'));
// views is the directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// The root url of the website serves the Angular app
app.get('/', function (request, response) {
response.render('pages/index');
});
// connect to database
mongoose.connect(config.database);
// pass passport for configuration
require('./config/passport')(passport);
var roomService = require('./controllers/roomservice.js');
roomService.setup(io);
// connect the api routes under /api/*
app.use('/api', apiRoutes);
// Start the Express app
server.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});
Here is controllers/roomservice.js
function RoomService(){
var socketio;
var Comment = require('../models/comment'); // get the mongoose model
var Question = require('../models/question'); // get the mongoose model
var Room = require('../models/room'); // get the mongoose model
function setup(io){
socketio = io;
socketio.on("connection", function(socket){
socket.on('joinRoom', function(msg){
console.log("joinRoom");
socket.join(msg.room)
Question.find({})
.exec(function(err, questions) {
for(var question in questions){
for(var comment in questions[question].comments){
questions[question].comments[comment] = Comment.find({ "_id": questions[question].comments[comment]});
}
}
socket.emit("listQuestions", questions)
});
});
socket.on('addQuestion', function(msg){
console.log("addQuestion");
var question = new Question({
title: msg.title,
question: msg.question
});
// save the question
question.save(function(err) {
if (err) throw err;
io.to(msg.room).emit("addQuestion", question);
});
});
socket.on('addComment', function(msg){
var comment = new Comment({
comment: msg.comment
});
// save the comment
Question.findOne({_id: msg.question}, function(err, question){
if (err) throw err;
question.comments.push(comment);
question.save(function(err) {
if (err) throw err;
io.to(msg.room).emit("addComment", comment);
console.log(question);
});
});
});
socket.on('addPlus', function(msg){
// save the comment
Question.findOne({_id: msg.question}, function(err, question){
if (err) throw err;
question.plus = question.plus + 1;
question.save(function(err) {
if (err) throw err;
io.to(msg.room).emit("addPlus", question);
});
});
});
socket.on('addMinus', function(msg){
// save the comment
Question.findOne({_id: msg.question}, function(err, question){
if (err) throw err;
question.minus = question.minus + 1;
question.save(function(err) {
if (err) throw err;
io.to(msg.room).emit("addMinus", question);
});
});
});
});
}
return{
setup: setup
}
}
module.exports = new RoomService();
I'm trying to populate the array of comments when returning the list of questions. I tried it with the populate method from Mongoose but it returns an empty array of comments.
Question.find({}).populate("comments")
.exec(function(err, questions) {
socket.emit("questions", questions)
});
When I don't put the populate method I get something like this :
[{"title": "test", "comments": ["1253454", "654654747"]},
{"title": "test", "comments": ["1253454", "654654747"]}]
But I want something like this :
[{"title": "test", "comments": [{"comment": "test"}, {"comment": "test2"}]},
{"title": "test", "comments": [{"comment": "test"}, {"comment": "test2"}]}]
What am I doing wrong?

What is your mongoose version? The version 4.7.1 is fine for me:
import { connDb } from './base';
import { model, Schema, SchemaTypes, disconnect } from 'mongoose';
const Question = model<any>('Question', new Schema({
title : SchemaTypes.String,
comments: [
{
type: SchemaTypes.ObjectId,
ref : 'Comment',
},
],
}))
const Comment = model<any>('Comment', new Schema({
comment: SchemaTypes.String,
}))
connDb().then(() => Promise.all([
Comment.remove({}),
Question.remove({}),
])).then(() => Comment.insertMany([
{
comment: 'hello',
},
{
comment: 'world',
}
])).then((data: any[]) => Question.insertMany([
{
title : 'question',
comments: data.map((item: any) => item._id),
}
])).then(() => Question.find({}).populate('comments').exec()).then((d) => {
console.log(JSON.stringify(d))
}).then(() => disconnect())
The result is:
+ ts-node ./src/__test__/pop_comment.ts
[{"_id":"5846322e69db6c1ec86ac5fd","__v":0,"title":"question","comments":[{"_id":"5846322e69db6c1ec86ac5fb","__v":0,"comment":"hello"},{"_id":"5846322e69db6c1ec86ac5fc","__v":0,"comment":"world"}]}]

Related

MongoDB .find problems NodeJS

When I attempt the code below, I just get an empty return from MongoDB....
let express = require('express');
let mongoose = require('mongoose');
let cors = require('cors');
let bodyParser = require('body-parser');
let testSchema = new mongoose.Schema({
username: String,
name: {
firstname: String,
lastname: String,
},
email: String,
employeeID: String
});
const testModel = mongoose.model('test', testSchema);
mongoose.connect('mongodb://localhost:27017/test', { useUnifiedTopology: true }, function(err, res) {
if (err) console.log(err);
else console.log('Connected to Mongo');
});
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cors());
const port = 5000;
const server = app.listen(port, () => {
console.log('connected to port ' + port);
});
testModel.find({}).exec(function(err, res){
if (!err) {
console.log(res);
}
else {
console.log("Error");
}
})
BUT, when I use this code, it returns the data I'm looking for..... why?! Every other tutorial I have seen operates like the above.
let express = require('express');
let mongoose = require('mongoose');
let cors = require('cors');
let bodyParser = require('body-parser');
let testSchema = new mongoose.Schema({
username: String,
name: {
firstname: String,
lastname: String,
},
email: String,
employeeID: String
});
const testModel = mongoose.model('test', testSchema, 'test');
mongoose.connect('mongodb://localhost:27017/test', { useUnifiedTopology: true }, function(err, res) {
if (err) console.log(err);
else console.log('Connected to Mongo');
});
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cors());
const port = 5000;
const server = app.listen(port, () => {
console.log('connected to port ' + port);
});
testModel.find({}).exec(function(err, res){
if (!err) {
console.log(res);
}
else {
console.log("Error");
}
})
How can I fix this in my code, or is it something within Mongo updates that this is a requirement?
By default, mongoose pluralizes the name of the model when you call mongoose.model() to determine the collection name. In your first example, it will be looking in the tests collection for documents.
In the second example, you specify that the name of the collection should be test , which is why the behavior is different.

mongoose required true req.body.name {}

If I set required to false, it will successfully create an object in the MongoDB database with one id. I suffer confusion sometimes, check my profile if you want. I think it's a little thing. If you need more info, just comment.
app.js
var express = require('express');
var bodyParser = require('body-parser');
var product = require('./routes/product'); // Imports routes for the products
var app = express();
var mongoose = require('mongoose'); // Set up mongoose connection
var dev_db_url = 'mongodb://localhost/Product';
var mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/products', product);
var port = 3002;
app.listen(port, () => {
console.log('Server is up on port numbner ' + port);
});
model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ProductSchema = new Schema({
name: {type: String, required: true, max: 100},
price: {type: Number, required: true},
});
module.exports = mongoose.model('Product', ProductSchema);
controller.js
var Product = require('../models/product');
//Simple version, without validation or sanitation
exports.test = function (req, res) {
res.send('Greetings from the Test controller!');
};
exports.product_create = function (req, res, next) {
var product = new Product(
{
name: req.body.name,
bags: req.body.bags
}
);
console.log(JSON.stringify(req.body))
product.save(function (err) {
if (err) {
return next(err);
}
res.send('Bags Created successfully')
})
};
router.js
var express = require('express');
var router = express.Router();
// Require the controllers WHICH WE DID NOT CREATE YET!!
var product_controller = require('../controllers/product');
// a simple test url to check that all of our files are communicating correctly.
router.get('/test', product_controller.test);
router.post('/create', product_controller.product_create);
module.exports = router;
HTTP POST: http://localhost:3002/products/create?name=Jorge&price=20
ValidationError: Product validation failed: name: Path name is
required
Can you help?
Thanks!
💡 The reason why it's error, because your req.body.name is empty or null. Why it's null or empty or undefined? Because you're not add your data in your body, when you send create request.
You can see your Endpoint:
HTTP POST: http://localhost:3002/products/create?name=Jorge&price=20
It's not about req.body, it's a req.params. So you can use req.params.name and req.params.price.
🕵️‍♂️ So, If you're passing your data using parameres, your code will looks like this:
exports.product_create = function (req, res, next) {
var product = new Product(
{
name: req.params.name,
price: req.params.price
}
);
console.log(req.params);
product.save(function (err) {
if (err) {
return next(err);
}
res.send('Bags Created successfully')
})
};
If you want to use req.body, than add your json object tobody if you're using Postman.
🕵️‍♂️ You can see the image below: An example using postman to passing your data into body, before you send create request to your backend.
So, If You're passing your data from body, than your code will looks like this:
exports.product_create = function (req, res, next) {
var product = new Product(
{
name: req.body.name,
price: req.body.price
}
);
console.log(req.body);
product.save(function (err) {
if (err) {
return next(err);
}
res.send('Bags Created successfully')
})
};
I hope it's can help you.

mongodb: Database not showing up in show dbs after data is inserted

I am following a YouTube tutorial from Jose Annunziato. I created my server.js and did all the required settings and configurations for my database connection. Now when I am posting something from the form to the server: it shows the data is sent to the server successfully but when I go to the mongo console to verify if the data is received and database is created or not. I run db it says test I run show dbs and there I can't see my new Database. I am not sure what the actual problem is because I did everything Jose said in the tutorial.
Server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/blogfall2016');
var PostSchema = mongoose.Schema({
title: {type: String, required: true},
body: String,
tag: {type: String, enum:['POLITICS', 'ECONOMY', 'EDUCATION']},
posted: {type: Date, default: Date.now},
});
var PostModel = mongoose.model('PostModel', PostSchema)
// GET /style.css etc
app.use(express.static(__dirname + '/public'));
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/api/blogpost", CreatePost);
function CreatePost(req, res) {
var post = req.body;
console.log(post);
PostModel.create(post);
res.json(post);
}
app.listen(3000);
If your schema and data you want to insert are matched then it should work.
Try below code. instead of PostModel.create(post);
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/nnnnnn', function (err) {
if (!err) {
console.log('connection successful');
} else {
console.log(err)
}
});
var PostSchema = mongoose.Schema({
title: { type: String, required: true },
body: String,
tag: { type: String, enum: ['POLITICS', 'ECONOMY', 'EDUCATION'] },
posted: { type: Date, default: Date.now },
});
var PostModel = mongoose.model('PostModel', PostSchema)
// GET /style.css etc
app.use(express.static(__dirname + '/public'));
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/api/blogpost", CreatePost);
function CreatePost(req, res) {
var post = req.body;
console.log(post);
// static data
var post = {
title: 'asdfasdf',
body: 'asdfasdfasdfasdf',
tag: 'POLITICS',
posted: new Date()
}
var postModel = new PostModel(post);
postModel.save(function (err, data) {
if (err) {
console.log('Error', err);
} else {
console.log('data inserted');
}
});
res.json(post);
}
app.listen(3000);
Instead of below line
mongoose.connect('mongodb://localhost/blogfall2016');
Try this one to make sure that database created successfully first,
then do your operation
mongoose.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});

How to register mongoose plugin for all schemas?

What am I missing? The Mongoose docs say that mongoose.plugin() registers a plugin for all schemas. This is not working. I CAN register my plugin on EACH schema.
My plugin:
module.exports = function (schema, options) {
schema.set('toObject',{
transform: function (doc, ret, options) {
return {
test: 'It worked!'
};
}
});
};
My schema:
var testPlugin = require('test-plugin.js');
var personSchema = mongoose.Schema({
_id : { type: String, default: $.uuid.init },
ssn : { type: String, required: true, trim: true },
first : { type: String, required: true, trim: true },
middle : { type: String, required: true, trim: true },
last : { type: String, required: true, trim: true }
});
personSchema.plugin(testPlugin);
var model = mongoose.model('Person', personSchema);
module.exports = model;
The code above works, unfortunately. However, the following code does not:
var personSchema = mongoose.Schema({
_id : { type: String, default: $.uuid.init },
ssn : { type: String, required: true, trim: true },
first : { type: String, required: true, trim: true },
middle : { type: String, required: true, trim: true },
last : { type: String, required: true, trim: true }
});
var model = mongoose.model('Person', personSchema);
module.exports = model;
My test app:
var testPlugin = require('test-plugin.js');
mongoose.plugin(testPlugin);
mongoose.Promise = global.Promise;
mongoose.connect(config.db);
mongoose.connection.on('error', function (err) {
if (err) { throw err; }
});
mongoose.connection.once('open', function (err) {
if (err) { throw err; }
seeds.doSeed(function(err){
if (err) { return process.exit(1); }
models.Person.find({}, function(err, people){
if (err) { throw err; }
var person = people[0];
var oPerson = person.toObject();
console.log(JSON.stringify(oPerson));
});
});
});
I've tried moving the mongoose.plugin(testPlugin) all over the app.js file... after the connect, etc... and nothing has worked.
Plugins might not be registered with mongoose.plugin(myMongoosePlugin) because mongoose models were created before you are registering plugins globally.
In case if you have expressjs routes:
Make sure that in your app.js (server.js) you are registering mongoose plugins before you are registering/creating expressjs routes (which are using mongoose models to communicate with database).
Example:
in app.js
const express = require(express);
const mongoose = require('mongoose');
const myMongoosePlugin = require('<Mongoose Plugin file path>');
mongoose.plugin(myMongoosePlugin);
let app = express();
//register expressjs routes
require('<Express routes file path>')(app, express.Router());
// or create expressjs routes
app.post('/person', (req, res, next) => {
//where someMethod is using person mongoose model
this.someController.someMethod(someArguments)
.then((user) => {
res.json(user);
}).catch((error) => {
next(error);
});
});
// ... Some other code ...
mongoose.connect(<databaseConnectionString>);
app.listen(<Port>);
Try requiring your model also in your app.js file. Somewhere after mongoose.plugin(testPlugin).
This is not the best solution but work , you must define your Schema in each file and then export that
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({ ... })
module.exports = UserSchema;
and then you should implement just one file for setup your models , for example
const mongoose = require('mongoose');
// import all of your schemas
const userSchema = require(./user/modelSchema);
const TicketSchema = require(./Ticket/modelSchema);
// *** implement your (Global Plugin) here ***
mongoose.plugin(myPlugin);
// define and create all of your models
const User = mongoose.model('User', userSchema);
const Ticket = mongoose.model('Ticket', TicketSchema);
module.exports = {
userModle: User,
ticketModel: Ticket,
}
This is just another view of what #Andrei Surzhan already stated.
The key here is to Apply the plugins before the Routes.
I recently got this issue, where the plugin's didn't work on a Global scale but it did when added them individually on the creation of their Schema.
For reference, my project is structured this way.
// server.js file
require('dotenv').config();
const http = require('http');
const mongoose = require('mongoose');
const myPlugin = require('path-to-my-plugin');
mongoose.plugin(myPlugin);
const app = require('./app');
const PORT = process.env.PORT || 8000;
const server = http.createServer(app);
// MONGO CONNECTION
mongoose.connect(process.env.MONGODB_URI);
mongoose.connection.on('open', () => console.log('MongoDB connection ready!'));
mongoose.connection.on('error', console.error);
server.listen(PORT, console.log(`Listening on PORT ${PORT}`));
// app.js file
const express = require('express');
// Routes
const clientsRouter = require('./routes/clients/clients.router');
const paymentsRouter = require('./routes/payments/payments.router');
const app = express();
// Parse incoming requests with JSON payloads
app.use(express.json());
// Parse incoming requests with Form payloads
app.use(
express.urlencoded({
extended: false,
})
);
app.use('/clients', clientsRouter);
app.use('/payments', paymentsRouter);
module.exports = app;
As you can see at server.js we add the plugin before even importing app.js this is because when we import app.js we will call the routes and the plugin will not be passed.
Another way to do it, is adding the plugin on each Schema.
Example
// clients.model.js
const mongoose = require('mongoose');
const myPlugin = require('path-to-my-plugin');
const clientSchema = new mongoose.Schema({ ... });
clientSchema.plugin(myPlugin);
module.exports = mongoose.model('Client', clientSchema);

Node.Js mongoose find not working

I'm trying to get the documents from my MongoDB by using mongoose "find" method. But I'm not able to get records. It did not returns any error. Here is my code
Server.js
var express = require('express');
var bodyparser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
mongoose.promise = global.promise;
mongoose.connect("mongodb://localhost:27017/testdb");
app.use(bodyparser.json());
app.use(cors());
app.use(express.static(__dirname + 'server'));
var api = require('./routes/apihandler')(app, express, io);
app.use('/alerts', api);
http.listen(3000, function(err){
if(err) {
console.log(err);
} else {
console.log('Listening PORT' + config.port);
}
});
apihandler.js
var request = require('request');
var express = require('express');
var alertRoutes = require('../../services/routes/alertRoutes');
var api = express.Router();
module.exports = function(app, express, io){
api.get('/getAllAlerts/:retailerId/:loyaltyId', getAllAlerts);
return api;
}
function getAllAlerts(req, res, callback) {
console.log('apihandler');
try {
alertRoutes.getAllAlerts(req,res,callback);
}
catch(err) {
res.send({"status" : "error", message: err.mes});
}
}
Manager.js
var alertModel = require('./../models/alertModel');
alertModel.find({}, function (err, docs) {
console.log(err);
console.log(docs);
});
Model.js
var mongoose = require('mongoose');
var alertSchema = new mongoose.Schema({
id:{ type: String },
alertType:{ type: Number},
retailerId: { type: String },
loyaltyId: { type: String },
email: { type: String },
description: { type: String},
locationId:{type: String },
reason:{type: String},
reasonDescription:{type: String},
capturedReasonsList:mongoose.Schema.Types.Mixed
},{ collection: 'alerts' });
module.exports = mongoose.model('alerts', alertSchema);
alertRoutes.js
//Get all Alerts
function getAllAlerts(req, res, callback){
console.log('getAllAlerts');
=
var srchData = {
retailerId:req.params.retailerId,
loyaltyId:req.params.loyaltyId
};
retailerId='+req.params.retailerId+'&loyaltyId='+req.params.loyaltyId;
alertManager.getAlert(srchData,function(err,alertDetails){
console.log('alertDetails',alertDetails);
if (err) throw err;
if(alertDetails && alertDetails.length > 0){
res.status(200).send(alertDetails);
}else{
res.status(200).send({success: false,message: 'Alert Details not Found'});
}
});
// });
}
I can able to get into Manager js. But the find query is not working
Please Help me to resolve this issue.
Thank you.
Try these codes. I am working with these codes & it works for me.
Model :
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var alertSchema = mongoose.Schema({
id:{ type: String },
alertType:{ type: Number},
retailerId: { type: String },
loyaltyId: { type: String },
email: { type: String },
description: { type: String},
locationId:{type: String },
reason:{type: String},
reasonDescription:{type: String},
capturedReasonsList:mongoose.Schema.Types.Mixed
});
mongoose.model('Alert',alertSchema);
Server Code :
var mongoose = require('mongoose');
var alertModel = mongoose.model('Alert');
alertModel.find().exec(function (err, docs) {
if(!err)
console.log(docs);
});
Maybe the find is just never called... Do some console.log before the find to make sure.
Provide the code of "getAlert", we cannot see where and how Manager.js is used.

Resources