Mongoosejs : Populate doesn't return any data - node.js

Hi I'm new in mongoose and I have a problem with populate collection it doesn't return any data, I don't know why here's my code:
Company Model
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Agent = mongoose.model('Agent'),
DBRef = mongoose.SchemaTypes.DBRef;
/**
* Company Schema
*/
var CompanySchema = new Schema({
name: {
type: String,
default: '',
//required: 'Please fill Company name',
trim: true
},
created: {
type: Date,
default: Date.now
},
updated: {
type: Date,
default: Date.now
},
address: {
type: String,
default: '',
//required: 'Please fill Company address',
trim: true
},
locked: {
type: Boolean,
default: true,
},
deleted: {
type: Boolean,
default: false,
},
logo: {
type: String,
default: '',
},
email: {
type: String,
default: '',
index: { unique: true }
//required: 'Please fill Company email',
},
tel: {
type: String,
default: '',
//required: 'Please fill Company tel',
},
fax: {
type: String,
default: '',
//required: 'Please fill Company fax',
},
type: {
type: String,
//required: 'Please fill Company type',
trim: true
},
description: {
type: String,
default: '',
trim: true
},
validator: {
type: Schema.ObjectId,
ref: 'User'
},
admins: [Agent]
});
module.exports = mongoose.model('Company', CompanySchema);
Agent Model
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
//Company = mongoose.model('Company'),
DBRef = mongoose.SchemaTypes.DBRef;
/**
* Agent Schema
*/
var AgentSchema = new Schema({
// Agent model fields
// ...
firstname: {
type: String,
default: ''
},
lastname: {
type: String,
default: ''
},
email: {
type: String,
default: '',
index: { unique: true }
},
password: {
type: String,
default: ''
},
roles: {
type: Array,
},
created: {
type: Date,
default: Date.now
},
updated: {
type: Date,
default: Date.now
},
deleted: {
type: Boolean,
default: false
},
locked: {
type: Boolean,
default: false
},
workFor: {
type: Schema.ObjectId,
ref: 'Company'
}
});
module.exports = mongoose.model('Agent', AgentSchema);
Populate Code
Company.findById(id).populate('admins').exec(function(err, company) {
if (err) return next(err);
if (!company) return next(new Error('Failed to load Company ' + id));
console.log(company.admins);
req.company = company ;
next();
});
thanks :) .

your schema definition of company actually defines that the agents are embedded into the company document, not referenced. If you want references that you then could populate you should use something like this:
var CompanySchema = new Schema({
....
admins: [{type: Schema.Types.ObjectId, ref: 'Agent'}],
....

thnks reto, here is the saving code of company :
/**
* Create a Company
*/
exports.create = function(req, res) {
var company = new Company(req.body);
User.findById('54e22d19aae0cff01a47df96',function(err,user){
if (err) {
return res.status(400).send({
messages: errorHandler.getErrorMessage(err)
});
}
company.user = user;
req.checkBody('name', 'please fill the name').notEmpty();
req.checkBody('address', 'please fill the address').notEmpty();
req.checkBody('tel', 'please fill a correct phone').notEmpty().isNumeric();
req.checkBody('type', 'please fill the type').notEmpty();
req.checkBody('email', 'please fill a correct email').isEmail();
req.checkBody('admin.firstname', 'please fill the admin firstname').notEmpty();
req.checkBody('admin.lastname', 'please fill the admin lastname').notEmpty();
req.checkBody('admin.email', 'please fill a correct admin email').isEmail();
req.checkBody('admin.password', 'please fill the admin password').notEmpty();
var errors = req.validationErrors();
if (errors) {
return res.status(400).send({
messages: errors
});
}else{
company.save(function(err) {
if (err) {
return res.status(400).send({
messages: errorHandler.getErrorMessage(err)
});
} else {
var agents = preapareAgents(req,company);
Agent.create(agents,function(err){
if(err){
return res.status(400).send({
messages: errorHandler.getErrorMessage(err)
});
}else{
res.jsonp({company: company, agents: agents});
}
});
}
});
}
});
};

Related

Sub-schema properties are not updated mongoose

little bit stuck with one issue which is related to mongoose. I have a Project schema with sub-schema(SingleUserSchema). Whenever I add a new User to this schema it saves everything and that's ok. The issue is that if I update the user, old values in schema does not update. Any ideas how to solve this case? Stuck here for a whole day already.
Schema:
const mongoose = require('mongoose');
const SingleUserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
},
role: {
type: String,
required: true,
},
status: {
type: String,
required: true,
},
});
const ProjectSchema = new mongoose.Schema(
{
name: {
type: String,
required: [true, 'Provide project name'],
minlength: 5,
},
description: {
type: String,
required: [true, 'Provide description about the project'],
},
maxWorkingEmployees: {
type: Number,
required: [
true,
'Provide maximum number of employees working on this project',
],
},
currentlyWorkingEmployees: [SingleUserSchema],
status: {
type: String,
enum: ['Pending', 'In progress', 'Paused', 'Delayed', 'Completed'],
default: 'Pending',
},
createdBy: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model('Project', ProjectSchema);
Controller:
const attachEmployeeToProject = async (req, res) => {
const { projectId, userId } = req.params;
const project = await Project.findOne({ _id: projectId });
const user = await User.findOne({ _id: userId });
if (!user) {
throw new NotFoundError(`User with id ${userId} does not exists`);
}
if (!project) {
throw new NotFoundError(`Project with id ${userId} does not exists`);
}
const { role, email, status } = user;
const SingleUserSchema = {
email,
role,
status,
};
let currentlyWorkingEmployees = [
...project.currentlyWorkingEmployees,
SingleUserSchema,
];
req.body.currentlyWorkingEmployees = currentlyWorkingEmployees;
const updateProject = await Project.findOneAndUpdate(
{ _id: projectId },
req.body,
{
new: true,
runValidators: true,
}
);
res.status(StatusCodes.OK).json({ updateProject });
};
Maybe I just simply need to create a reference? Tried like this and received tons of errors, also felt lost how to create n references in array.
currentlyWorkingEmployees: [
{ type: mongoose.Schema.ObjectId, ref: 'User', required: true },
],
User schema:
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: [true, 'Please provide username'],
minlength: 3,
maxlength: 50,
},
email: {
type: String,
required: [true, 'Please provide username'],
unique: true,
validate: {
validator: validator.isEmail,
message: 'Please provide valid email address',
},
},
password: {
type: String,
required: [true, 'Please provide password'],
validator: {
validate: {
validator: validator.isStrongPassword,
message: 'Please provide stronger password',
},
},
},
firstname: {
type: String,
required: [true, 'Please provide first name'],
},
lastname: {
type: String,
required: [true, 'Please provide last name'],
},
status: {
type: String,
enum: ['Working', 'Pause', 'Offline'],
default: 'Offline',
},
role: {
type: String,
enum: [
'Developer',
'Analyst',
'Designer',
'Architect',
'Project Manager',
'Owner',
'Teamleader',
'Employee',
],
default: 'Employee',
},
verificationToken: {
type: String,
},
isTokenVerified: {
type: Boolean,
default: false,
},
tokenValidationDate: {
type: Date,
default: null,
},
});
First, as far as I see you don't have a model called 'User' so that's the reason why your reference is not working.
currentlyWorkingEmployees: [
{ type: mongoose.Schema.ObjectId, ref: 'User', required: true },
]
https://mongoosejs.com/docs/populate.html
Second, you need to identify the user you want to update (based on userId) inside currentlyWorkingEmployees collection, if I understood correctly your problem.
Hope it helps!

How to push objectId to mongodb schema field array of object using findByIdAndUpdate

Everytime I hit api I am getting same error-
I have tried sending value as parameters also but failed. Any help would be appreciated. When i use $set it updates same value everytime the web service is called but it does work but not with $push.
MongoError: The field 'songId' must be an array but is of type objectId in document
{_id: ObjectId('59709590380026118c22dd61')}
My Playlist schema code:-
var PlaylistSchema = new mongoose.Schema({
name: String,
coverphoto: { type: String, default: '' },
userId: {
type: ObjectId,
ref: 'User'
},
songId: [{ type: ObjectId, ref: 'Song' }],
updated_at: { type: Date, default: Date.now },
});
my Song Schema code
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var SongSchema = new mongoose.Schema({
audioName:{type:String,default:''},
title: String,
// coverphoto: { type: String, default: '' },
singer: { type: String, default: '' },
movie: { type: String, default: '' },
album: { type: String, default: '' },
lyricist: { type: String, default: '' },
actors: { type: String, default: '' },
lyrics: { type: String, default: '' },
genre: { type: String, default: 'Random' },
duration: { type: String, default: '' },
size: { type: String, default: '' },
userId: {
type: ObjectId,
ref: 'User'
},
categoryId: {
type: ObjectId,
ref: 'Category'
},
updated_at: { type: Date, default: Date.now },
});
module.exports = mongoose.model('Song', SongSchema);
My Api code
/* post api to add song in playlist */
router.post('/addSongToPlaylist', function (req, res, next) {
Playlist.findOne({ '_id': req.body.playlistId }, function (err, playlist) {
if (err) return next(err);
console.log(playlist)
console.log("req.body.songId", req.body.songId);
if (playlist) {
Playlist.findByIdAndUpdate(
req.body.playlistId,
{ $push: { "songId": req.body.songId } },
{ new: true },
function (err, playlistData) {
if (err) return next(err);
res.json({ message: 'New Song added successfully', playlist: playlistData, success: true });
});
} else if (!Song) {
res.json({ message: 'Failed to add song', success: false });
}
});
});

Load specific mongoose models first

I'm attempting to use a model ("goal") in my "like" model class (Code below). However every time I startup the node instance it says that Goal.findById is not a function.
After running a console.log(number) in each model, I found that they load very oddly and out of the order I would like/need.
I was wondering how I can load models before others or set a specific load order for models?
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = require('../models/user.js');
var Goal = require('../models/goal.js');
var likeSchema = new Schema({
userPosted: {
type: Number,
ref: 'user',
required: true
},
goal: {
type: Number,
ref: 'goal',
required: true
}},
{
timestamps: true
});
likeSchema.post('save', function (doc, next) {
var goalID = doc.goal;
Goal.findOne({'_id': doc.goal}, function(err, goal) {
goal.likes.push(doc._id);
goal.save();
User.findById(doc.userPosted, function(err, user) {
user.likedPosts.push(goalID);
user.save();
next();
});
});
});
likeSchema.post('remove', function(doc) {
Goal.findById(doc.goal, function(err, goal) {
goal.likes.pull(doc._id);
goal.save();
User.findById(doc.userPosted, function(err, user) {
user.likedPosts.pull(goal._id);
user.save();
});
});
});
console.log("4");
module.exports = mongoose.model('like', likeSchema);
Error:
TypeError: Goal.findOne is not a function at model.<anonymous> (E:\Project\like.js:28:10)
**Edit: ** goal.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var autoIncrement = require('mongoose-auto-increment');
var User = require('../models/user.js');
var Like = require('../models/like.js');
var goal = new Schema({
user: {
type: Number,
ref: 'user',
required: true
},
user_name: {
type: String,
required: true
},
title: {
type: String,
required: true,
trim: true
},
description: {
type: String,
default: undefined,
trim: true
},
location: {
type: String,
default: undefined
},
likes: [{
type: Schema.ObjectId,
ref: 'like'
}],
comments: [{
type: Schema.ObjectId,
ref: 'comment'
}],
updates: [{
type: Schema.ObjectId,
ref: 'update'
}],
created: {
type: Date,
default: Date.now
},
cover_image: {
type: String,
default: undefined
},
complete_by: {
type: String,
default: "Death"
},
completed: {
type: Boolean,
default: false
},
completedDate: {
type: String,
default: undefined
},
url: {
type: String,
default: undefined
},
sponsor: {
type: String,
default: undefined
},
private: {
type: Boolean,
default: false
}
});
goal.plugin(autoIncrement.plugin, 'goal');
goal.pre('save', function (next) {
this.wasNew = this.isNew;
next();
});
goal.post('save', function (doc) {
if (this.wasNew) {
User.findById(doc.user, function (err, user) {
user.goals.push(doc._id);
user.save();
});
}
});
goal.post('remove', function(doc) {
//TODO: Removes like objects/user profile/anything containing the goal
User.findById(doc.user, function(err, user) {
user.goals.pull(doc._id);
user.save();
});
Like.find({
'goal': doc._id
}).remove(function(err, removed) {
if(err) {
console.log("ERROR?");
return;
}
console.log('removed likes - ' + removed);
});
});
module.exports = mongoose.model('goal', goal);
I think your likeschema would be something like this -
var likeSchema = new Schema({
userPosted: {
type: Number,
ref: 'User',
required: true
},
goal: {
type: Number,
ref: 'Goal',
required: true
}},
{
timestamps: true
});
If it doesn't work, can you please show your code for Goal model?

mongoose js populate embedded objects in array

I am trying to display a list of subject names associated with a training event, however the object id is the only property that is populated after the query has executed. I have tried the solution in this QUESTION which seems to be similar to what I am trying to achieve, but it is not working.
My load method is at the very bottom of this code snippet.
Any help is appreciated, thanks.
var UserSchema = new Schema({
firstName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
displayName: {
type: String,
trim: true
}
});
var SubjectSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill subject name',
trim: true
},
description: {
type: String,
default: '',
required: 'Please fill subject description',
trim: true
}
});
var TrainingSubjectSchema = new Schema({
subject: {
type: Schema.ObjectId,
ref: 'Subject'
},
attendanceRate:[{
type: Number
}]
});
var TrainingEventSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill training event name',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
attendees:[{
type: Schema.ObjectId,
ref: 'User'
}],
subjects:[TrainingSubjectSchema]
});
// retrieve method
Training.findById(id).populate('user', 'displayName').populate('subjects.subject','name').exec(function(err, trainingevent)
Not sure why it is not working in your side but I have tried
in my code and it is working as expected.
Here it is how I am creating the Schemas:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost:27017/mongoose_benn');
var UserSchema = new Schema({
firstName: {type: String, trim: true, default: ''},
lastName: {type: String, trim: true, default: ''},
displayName: {type: String, trim: true}
});
var SubjectSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill the subject name.',
trim: true
},
description: {
type: String,
default: '',
required: 'Please fill subject description',
trim: true
}
});
var TrainingSubjectSchema = new Schema({
subject: {type: Schema.ObjectId, ref: 'Subject'},
attendanceRate: [{type: Number}]
});
var TrainingEventSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill the training event name.',
trim: true
},
created: {type: Date, default: Date.now},
user: {type: Schema.ObjectId, ref: 'User'},
attendes: [{
type: Schema.ObjectId,
ref: 'User'
}],
subjects: [TrainingSubjectSchema]
});
module.exports = {
User: mongoose.model('User', UserSchema),
Subject: mongoose.model('Subject', SubjectSchema),
TrainingSubject: mongoose.model('TrainingSubject', TrainingSubjectSchema),
TrainingEvent: mongoose.model('TrainingEvent', TrainingEventSchema)
};
And here I am processing data with the models:
var async = require('async');
var models = require('./models.js');
var User = models.User;
var Subject = models.Subject;
var TrainingSubject = models.TrainingSubject;
var TrainingEvent = models.TrainingEvent;
async.waterfall(
[
clearDB.bind(null, {collections: [User, Subject, TrainingSubject, TrainingEvent], async: async}),
createUsers.bind(null, {User: User, async: async}),
createSubjects.bind(null, {async: async, Subject: Subject}),
createTrainingEvents.bind(null, {async: async, TrainingEvent: TrainingEvent}),
showTrainingEvents.bind(null, {TrainingEvent: TrainingEvent})
]
);
function createTrainingEvents(data, db, next) {
var firstSubject = db.subjects[0];
var secondSubject = db.subjects[1];
var trainingSubjects = [
{
subject: firstSubject._id,
attendanceRate: [5, 5]
},
{
subject: secondSubject._id,
attendanceRate: [4, 4, 5]
}
];
var trainingEvents = [
{
name: 'October Fest',
user: db.users[0]._id,
subjects: [trainingSubjects[0]],
attendes: [db.users[0]._id, db.users[1]._id]
},
{
name: 'August Fest',
user: db.users[1]._id,
subjects: [trainingSubjects[1]],
attendes: [db.users[0]._id, db.users[1]._id]
}
];
data.async.map(
trainingEvents,
function(trainingEvent, done) {
data.TrainingEvent.create(trainingEvent, done);
},
function(err, result) {
next(err);
}
);
}
function clearDB(data, next) {
async.map(
data.collections,
function(collection, done) {
collection.remove({}, done);
},
function(err) {
next(err);
}
);
}
function createUsers(data, next) {
var users = [
{firstName: 'Wilson', lastName: 'Balderrama', displayName: 'Wily'},
{firstName: 'Santiago', lastName: 'Balderrama', displayName: 'Santi'},
{firstName: 'Keila', lastName: 'Balderrama', displayName: 'Kei'}
];
data.async.map(
users,
function(user, done) {
data.User.create(user, done);
},
function(err, results) {
next(err, {users: results});
}
);
}
function showUsers(data, next) {
data.User.find({}, function(err, users) {
next();
});
}
function createSubjects(data, db, next) {
var subjects = [
{
name: 'JavaScript for Beginners',
description: 'JS fundamentals'
},
{
name: 'Node.JS for Beginners',
description: 'Node.JS streams'
}
];
data.async.map(
subjects,
function(subject, done) {
data.Subject.create(subject, done);
},
function(err, result) {
db.subjects = result;
next(err, db);
}
);
}
function createTrainingSubjects(data, db, next) {
var firstSubject = db.subjects[0];
var secondSubject = db.subjects[1];
var trainingSubjects = [
{
subject: firstSubject._id,
attendanceRate: [5, 5]
},
{
subject: secondSubject._id,
attendanceRate: [4, 4, 5]
}
];
data.async.map(
trainingSubjects,
function(trainingSubject, done) {
data.TrainingSubject.create(trainingSubject, done);
},
function(err, result) {
db.trainingSubjects = result;
next(err, db);
}
);
}
function showTrainingSubjects(data, next) {
data.TrainingSubject
.find({})
.populate('subject')
.exec(function(err, data) {
next();
});
}
function showSubjects(data, next) {
data.Subject.find({}, function(err, data) {
next();
});
}
function showTrainingEvents(data, next) {
data.TrainingEvent
.find({name: 'August Fest'})
.populate('user', 'displayName')
.populate('subjects.subject', 'name')
.exec(function(err, data) {
console.log(JSON.stringify(data));
next();
});
}
And the output is;
[
{
"_id":"55981d58cfd48c905c3a5fde",
"user":{
"_id":"55981d58cfd48c905c3a5fd8",
"displayName":"Santi"
},
"__v":0,
"subjects":[
{
"subject":{
"_id":"55981d58cfd48c905c3a5fdb",
"name":"Node.JS for Beginners"
},
"_id":"55981d58cfd48c905c3a5fdf",
"attendanceRate":[
4,
4,
5
]
}
],
"attendes":[
"55981d58cfd48c905c3a5fd7",
"55981d58cfd48c905c3a5fd8"
],
"created":"2015-0704T17:52:24.181Z",
"name":"August Fest"
}
]

How to save multilanguage data in moongoose?

i try to learn Mean.js and i need to use multilanguage support in my app.
I chose this schema to save data
"_id": {
"name": {
en: "English",
fr: "Français"
}
}
in my server model:
var KnowledgeSchema = new Schema({
name: {
en: {
type: String,
default: '',
required: 'Please fill Knowledge name',
trim: true
},
fr : {
type: String,
default: '',
required: 'Please fill Knowledge name',
trim: true
}
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Knowledge', KnowledgeSchema);
and controller
exports.create = function(req, res) {
var knowledge = new Knowledge(req.body);
knowledge.user = req.user;
knowledge.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(knowledge);
}
});
};
What i need for save "en" and "fr" into "name" ?

Resources