Trying to update nested / subdocument in Express - node.js

I have a Mongoose model that has nested array and a subdocument.
I seem to be ok when posting to the object arrays/subdocument, but I'm having trouble with the .put
I hard coded the params for testing, just in case they were not coming in from PostMan for some reason.
The result I get from the above code is an empty array!
So I'm getting the right record and it creates the "phone" array, but does not populate.
.put(function(req, res){
Member.find({'_id':req.params.id}, function(err, member){
if(err)
res.send(err);
member.phone.push({
number: "78787878787",
phoneType: 2
})
member.save(function(err){
if(err)
res.send(err);
res.json(member);
});
});
});
I want to have an endpoint that simply adds another "phone" record.
Here is my model:
//DEPENDENCIES
var mongoose = require('mongoose');
var contactSchema = new mongoose.Schema({
name:{type:String},
age:{type:Number}
});
var phoneSchema = new mongoose.Schema({
number:{ type: String },
phoneType:{ type: Number }
})
var memberSchema = new mongoose.Schema({
firstname: {
type: String
},
lastname: {
type: String
},
phone:[phoneSchema],
contacts:[contactSchema]
});
//RETURN MODEL
module.exports = mongoose.model('member', memberSchema);
Now when I run my code I get the following undefined for "members":
{ id: '587bcbffe64e9f28a6894dd7' }
[ { _id: 587bcbffe64e9f28a6894dd7,
lastname: 'Stanley',
firstname: 'Dave',
__v: 0,
contacts: [ [Object] ],
phone: [] } ]
events.js:154
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'push' of undefined

find returns an array of documents, not just one document. Thats is why it is giving error when you are trying to do member.phone.
Use findOne instead of find as you are querying by _id, it will return only one matched document or null(if its not present), so its a better choice than find.
Also, its better to check if the result is null or not. member will be null if no such _id is present.
Member.findOne({'_id':req.params.id}, function(err, member){
if(err)
res.send(err);
else if(member!=null)
{
member.phone.push({
number: "78787878787",
phoneType: 2
});
member.save(function(err){...});
}
});
If you are keen on using find. Use member[0] (first element) instead of member.
Member.find({'_id':req.params.id}, function(err, member){
if(err)
res.send(err);
else if(member.length!=0)
{
member[0].phone.push({
number: "78787878787",
phoneType: 2
});
member[0].save(function(err){...});
}
});
Hope that helps you.

Related

Store value of a subquery - mongoose

What im doing:
When I call getData() the backend server .find() all my data.
My documents:
My test document has an _id a name and stuff fields. The stuff field contains the _id to the data document.
My data document has an _id and a age field
My goal:
When I send the data to the frontend I don´t want the stuff field to appear with the _id, I want it to appear with the age field from the correspondingdata.
What I have:
router.route('/data').get((req, res) => {
Test.find((err, aval) => {
if (err)
console.log(err);
else{
var result = [];
aval.forEach(e => {
var age;
// Get the age, only 1
Data.findById(e.stuff, 'age', function (err, a) {
age = a.age;
});
result.push({name: e.name, age: age});
});
res.json(result);
}
});
});
I find all the test documents then, for each one of them, I find the age and put the result in the array. Finaly I send the result array.
My problem:
The age field on my result array is always undefined, why? Any solutions?
UPDATE 1 - The schemas
The test schema
var TestSchema = new Schema(
{
stuff: {type: Schema.Types.ObjectId, ref: 'Data', required: true},
name: {type: String, required: true}
}
);
The data schema
var DataSchema = new Schema(
{
age: {type: Number, required: true}
}
);
router.route('/data').get((req, res) => {
Test.find({})
.populate('stuff')
.exec((err, aval) => {
if (err) console.log(err);
res.json(aval);
});
});
Mongoose model has a populate property that uses the value in the model attribute definition to get data matching the _id from another model.
It's a scop problem with your code try this out :
Data.findById(e.stuff, 'age', function (err, a) {
result.push({name: e.name, age: a.age});
});
But as a better solution think to use the Aggregation Framework

Get the _id of the sub-document from mongoose findOne query

The schema of my Sample model is:-
var nameSchema = new mongoose.Schema({
firstname:String,
lastname:String
})
var sampleSchema= new mongoose.Schema({
number: {
type: String
},
name :{
type : [nameSchema]
}
});
I am trying to update the first and last name by searching them by their number property by making use of Sample.findOne({number:number}). And i am performing the update operation in the following manner:-
module.exports.updateNumber = function(req, res){
var number= req.body.number;
var lname= req.body.lname;
var fname= req.body.fname;
Sample
.findOne({number:number})
.select('name')
.exec(function(err, doc){
console.log(doc)
var this_id;
var thisService = doc.name.id('this_id');
thisService.firstname=fname;
thisService.lastname=lname;
doc.save(function(err, update) {
if (err) {
res
.status(500)
.json(err);
} else {
res
res.render('done')
}
});
})
}
If i console log the output i got is:
{ _id: 5bc5d71f47ff14361c0639d1,
name:
[ { _id: 5bc5d71f47ff14361c0639d2,
firstname: 'firstname',
lastname: 'lastname' } ] }
Is there any way, i could store _id: 5bc5d71f47ff14361c0639d2 in 'this_id' variable, so that this updation would be possible
name is an array, so if you want the first _id then name[0]._id would suffice, if you want an array of all values for _id in name, then name.map((person) => person._id) would give you an array of _id
However, more details about the context of this object would help give a better answer.

Mongoose NodeJS Schema with array of ref's

I know there is allot's of answers about it but still I didn't quite get the idea.
I have CourseSchema:
const CourseSchema = new Schema({
course_name: String,
course_number: {type: String, unique : true },
enrolledStudents:[{
type: mongoose.Schema.Types.ObjectId,
ref: 'Student' }]
});
And a StudentSchema:
const StudentSchema = new Schema({
first_name: String,
last_name: String,
enrolledCourses:[{
type: mongoose.Schema.Types.ObjectId,
ref: 'CourseSchema'
}]
});
I want to reffer enrolledStudents at CourseSchema with a student, and enrolledCourses at StudentSchema with a course.
router.post('/addStudentToCourse', function (req, res) {
Course.findById(req.params.courseId, function(err, course){
course.enrolledStudents.push(Student.findById(req.params.studentId, function(error, student){
student.enrolledCourses.push(course).save();
})).save();
});
});
but when posting I get an error:
TypeError: Cannot read property 'enrolledStudents' of null
Ok so after readying Query-populate I did that:
router.post('/addStudentToCourse', function (req, res) {
Course.
findOne({ _id : req.body.courseId }).
populate({
path: 'enrolledStudents'
, match: { _id : req.body.studentId }
}).
exec(function (err, course) {
if (err) return handleError(err);
console.log('The course name is %s', course.course_name);
});
});
And when i'm hitting POST on postman I get on the console:
The course name is intro for cs
but it is loading for ever and later on console I get:
POST /courses/addStudentToCourse - - ms - -
You are missing the populate instruction. For example:
see more about it here
Course.
findOne({ courseId : req.params.courseId }).
populate('enrolledStudents').
exec(function (err, course) {
if (err) return handleError(err);
console.log('The course name is %s', course.name);
});
It is working by using the ref field that "knows" how to populate withput using the push syntax. it is like a foreign key population.
Just call the populate method on the query and an array of documents will be returned in place of the original _ids. you can learn more on the internals of the populate methods in the official docs

Catch error when using populate with mongoose

I have the next model and route with mongoose:
In my colection I have some invalids id's to "cidade" field and this is why I am getting the error showing below.
The error happens in the line:
.populate('cidade')
Is there a way to execute my router(code is below) in:
router.get('/:id',function(req,res,next){ .....
without stop on that error?
If an invalid "id" is found, I´d just like to ignore it and proceed to next.
My collections are too big and can have some invalids "ids" to "cidade" field.
//error
angular.js:14328 Possibly unhandled rejection: {"data":{"message":"Cast to ObjectId failed for value \"Ararendá\" at path \"_id\" for model \"Cidade\"","name":"CastError","stringValue":"\"Ararendá\"","kind":"ObjectId","value":"Ararendá","path":"_id"},"status":500,"config":
//models and route
//cidade
cidadesSchema = new mongoose.Schema({
uf: {type: String, unique:true},
cidade: {type: String, unique:true}
});
module.exports = mongoose.model('Cidade', cidadesSchema,'cidades' );
//profiss
var profissionaisSchema = new mongoose.Schema({
nome: {type: String, unique:true},
cidade: {type:mongoose.Schema.Types.ObjectId, ref:'Cidade'},
estado: {type:mongoose.Schema.Types.ObjectId, ref:'Estado'},
cep: {type: String},
});
module.exports = mongoose.model('Profissional', profissionaisSchema,'profissionais' );
//route
const callback=function(err,data,res){
if (err) return res.status(500).json(err);
return res.status(200).send(data);
}
router.get('/:id',function(req,res,next){
const query=req.params.id;
Profissional.findById(query).populate('profissao')
.populate('cidade')
.exec( (err,data) => {
callback(err,data,res)
});
});
I don't think you can tell Mongoose to just ignore those errors and keep going, so you're going to have to implement the population yourself (which should be relatively easy because you're using findById which would only yield, at most, one document).
Here's some (untested) code:
Profissional.findById(query).populate('profissao').exec( (err, profi) => {
if (err) {
return res.status(500).json(err);
} else if (! profi || ! /^[a-f0-9]{24}$/i.test(profi.cidade)) {
return res.status(200).send(profi);
}
Cidade.findById(profi.cidade).exec((err, cidade) => {
if (err) {
return res.status(500).json(err);
}
profi.cidade = cidade;
return res.status(200).send(profi);
});
});
If the cidade property looks like a valid ObjectId, it will run a query to retrieve it, otherwise it won't bother.

TypeError: Object.keys called on non-object when updating document

I'm trying to update an existing document by increment a counter and pushing an object to an array.
Here's the schema for User:
var UserSchema = new Schema({
[...]
posts: {
totalWords: { type: Number, min: 0, default: 0 },
_entries: [
{
words: { type: Number, min: 0 },
body: { type: String },
date: Date
}
]
},
});
And here's the update code:
var newPost = {
words: req.body.words,
body: req.body.entry,
date: new Date()
};
User.findOne(req.user._id, function (err, user) {
var previous = user.posts.totalWords;
user.posts.totalWords = previous + newPost.words;
user.posts._entries.push(newPost);
user.save(function (err) {
if (err) return res.send(400, err);
return res.json(newPost);
});
});
I get the following error:
[TypeError: Object.keys called on non-object]
Any ideas on how to solve this?
Answering my own question
I was able to solve the problem by changing:
User.findOne(req.user._id, function (err, user) { [...] });
Into this:
User.findById(req.user._id, function (err, user) { [...] });
I think, if you would like to use findOne you need follow syntax:
User.findOne({'_id': req.user._id}, {}, function (err, user) {
...
});
Not sure about findById() vs. findOne(), but I've had problems with Mongoose objects returning Object.keys called on non-object while saving or updating with mal-formed data or data corresponding to an older schema. While initializing the document, Mongoose was expecting a subdocument of some kind, but the data in the database didn't match that.
For me it usually happens when I change schemas from a simple object (String, Number, etc.) to a more complex subdocument of some kind. The schema types are mixed up and the object won't load, not even to fix the problem. I had to go into my database using the native driver, search for mal-formed documents using the $type operator, and update them individually.

Resources