Atache Atache - 6 months ago 20
Node.js Question

When I try to pass data to a response in Node I get: "error":{"message":"Cannot read property 'length' of undefined",

Whenever I try to pass any data to a response in Node i am getting the above error. Here is my code for my show action:

const show = (req, res, next) => {
User.findOne({"_id": req.currentUser._id}).then(function (user){
console.log(user.events.id(req.params.id));
return user.events.id(req.params.id);
return concert;
})
.then(concert => concert ? res.json({ concert }) : next())
.catch(err => next(err));
};


The console.log returns exactly what I would expect. If I change res.json to res.sendStatus('200') I get a 200 back. I think that the error is stemming from when the response tries to calculate the length of the body that it is returning.

Stack Trace Says:

Trace
at /Users/louisarnos/wdi/projects/capstone-project/capstone-backend/app/controllers/events.js:22:12
at process._tickCallback (node.js:369:9)
TypeError: Cannot read property 'length' of undefined
at EmbeddedDocument.length (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/app/models/event.js:48:19)
at VirtualType.applyGetters (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/node_modules/mongoose/lib/virtualtype.js:77:25)
at EmbeddedDocument.Document.get (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/node_modules/mongoose/lib/document.js:870:18)
at applyGetters (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/node_modules/mongoose/lib/document.js:2173:35)
at EmbeddedDocument.Document.$toObject (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/node_modules/mongoose/lib/document.js:1957:5)
at EmbeddedDocument.Document.toJSON (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/node_modules/mongoose/lib/document.js:2199:15)
at Object.stringify (native)
at ServerResponse.json (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/node_modules/express/lib/response.js:242:19)
at ServerResponse.send (/Users/louisarnos/wdi/projects/capstone-project/capstone-backend/node_modules/express/lib/response.js:151:21)
at /Users/louisarnos/wdi/projects/capstone-project/capstone-backend/app/controllers/events.js:25:36
at process._tickCallback (node.js:369:9)


Here is my user schema:

const userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
},
token: {
type: String,
require: true,
},
events: [EventSchema],
passwordDigest: String,
}, {
timestamps: true,
});


And my Events schema:

'use strict';

const mongoose = require('mongoose');

const eventSchema = new mongoose.Schema({
artist: {
type: String,
required: true,
},
location: {
venue: {
name: {
type: String,
required: true,
},
city: {
type: String,
required: true,
},
region: {
type: String,
required: true,
}
}
},
date: {
month: {
type: String,
required: true,
},
day: {
name: {
type: String,
required: true,
},
num_of_day: {
type: Number,
required: true,
}
}
}
}, {
timestamps: true,
toJSON: { virtuals: true },
});

eventSchema.virtual('length').get(function length() {
return this.text.length;
});

const Event = mongoose.model('Event', eventSchema);

module.exports = Event;

Answer

This is where the error is thrown:

eventSchema.virtual('length').get(function length() {
  return this.text.length;
});

text isn't a property defined in your schema, so it's undefined.