Cannot read property 'substring' of undefined



I had this problem a few weeks ago, then it went away, and now its back. I have two workspaces, my main one and a clone of that. The code runs fine in the clone but running the same exact code in my main workspace gives me the error 'Cannot read property ‘substring’ of undefined’
Because the same exact code works in one workspace and not in the other, I believe the problem is with my environment.
Any idea how to fix this?


Maybe you have added environment variables in one workspace that are missing in the other.
Just the generic error message, without stakctrace, and without description of what you do to get it, doesn’t provide enough information to help you


Here is my code:

<% blogs.forEach(function(blog){ %>
    <div class="row"> 
       <div class="col-lg-12 col-xs-12 text-left">
			<h2><%= blog.title %></h2>
			    <p> <strong><></strong>
    			 	 | <i class="glyphicon glyphicon-calendar"></i> <%= blog.created.toDateString() %>
				     | <i class="glyphicon glyphicon-comment"></i> <a href="/blogs/<%=blog._id%>"> <%= blog.comments.length %> Comments </a>       						
   				 <p> <%- blog.body.substring(0, 200)  %>. . .</p> // This is where the issue is
    			 		    <a href="/blogs/<%= blog._id %>">
    			 		    <button class="btn btn-default btn-sm">Read More</a>
		  <% }); %>


router.get("/blogs", function(req, res){
Blog.find({}).sort({_id:-1}).limit(5).populate("comments").exec(function(err, blogs){ 
	} else {
		res.render("index", {blogs: blogs, currentUser: req.user});


var blogSchema = new mongoose.Schema({
title: String,
body: String,
author: {
	id: {
		type: mongoose.Schema.Types.ObjectId,
		ref: "User"
	username: String,

comments: [
		type: mongoose.Schema.Types.ObjectId,
		ref: "Comment"
created: {type: Date, default:} 
}); module.exports = mongoose.model("Blog", blogSchema);

The problem i get is:

Cannot read property ‘substring’ of undefined
at eval (eval at (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:524:12), :82:26)
at Array.forEach (native)
at eval (eval at (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:524:12), :65:14)
at returnedFn (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:555:17)
at tryHandleCache (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:203:34)
at View.exports.renderFile [as engine] (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:412:10)
at View.render (/home/ubuntu/workspace/node_modules/express/lib/view.js:128:8)
at tryRender (/home/ubuntu/workspace/node_modules/express/lib/application.js:640:10)
at EventEmitter.render (/home/ubuntu/workspace/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/ubuntu/workspace/node_modules/express/lib/response.js:966:7)
at /home/ubuntu/workspace/routes/blogs.js:12:8
at /home/ubuntu/workspace/node_modules/mongoose/lib/query.js:2741:18
at newTickHandler (/home/ubuntu/workspace/node_modules/mongoose/node_modules/mpromise/lib/promise.js:234:18)
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)

I went through each workspace and the code is the same. I hope this info is enough! Thank you!


the data must be misformed or missing in the database of workspace with the error


I figured it out! It was an issue with the data. Thank you!


Hi, can you share details please? I have the exact same problem. Thank you.


i was trying to find the substring of a blog post that didnt have a body. Just clear your database or find the object that doesnt have a body property and delete it and you should be set.


Hi. I had the same issue. This worked for me:

<% if (typeof blog.body == 'undefined' ) { %>
            <p> no content</p>  
        <% } else { %>
            <p> <%= blog.body.substring(0, 100) %>...</p>  
 <% } %>