0

I am trying to access body inside blog schema. How can I do it.

Schema:

var ArticleSchema = new mongoose.Schema({
  blog: [{
    topic: { type: String, unique: false, lowercase: true },
    body: {  type: String, unique: false, lowercase: true },
    tags: [ 'first', 'mongodb', 'express'],
    created: Date,
    modified: { type : Date, default : Date.now },
    state: {  type: String, unique: false, lowercase: true }
    }]
});

**

router

**

router.get('/blog/article/:postid', function (req, res, next) {
  Article.findById({ _id: req.params.postid }, function (err, article) {
    if (err) return next(err);
    res.render('main/publishedArticle', {
      article: article,
      message: req.flash('showing article ' + article.title)
    });
  });
});

**

publishedArticle.ejs

**

<h3><%= article.blog.body %></h3>

I am am getting undefined

1 Answer 1

1

You've declared your blog schema as an array of objects (note that you've used the [ ] around the object for a blog item). If that was intentional, then you need to access various blog elements of your article with an array index (or iterate over them using a loop). The snippet below assumes that your article has at least one blog entry saved:

<h3><%= article.blog[0].body %></h3>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, looks so simple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.