1

I have 2 models.

Model 1:

const userSchema = new mongoose.Schema({
  email: { type: String, unique: true, required: true },
  password: { type: String, required: true },
  passwordResetToken: String,
  passwordResetExpires: Date,

  facebook: String,
  twitter: String,
  tokens: Array,

  profile: {
    name: String,
    gender: String,
    location: String,
    website: String,
    picture: String
  }
}, { timestamps: true });

Model 2:

const reviveSchema = new mongoose.Schema({
  reviveShowName: {type: String, required: true},
  reviveTitle: {type: String, required: true},
  reviveCategory: {type: String, required: true},
  reviveGoal: {type: Number, required: true},
  revivePhoto: {type: String, required: true},
  reviveVideo: {type: String},
  reviveStory: {type: String},
  author: {
      id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
      }
    }
}, { timestamps: true });

How can I pass the author's name to the show view of a particular revive?

This is how I was getting to the show view before I realized that I needed the author's data to be passed to the view as well:

exports.showRevive = (req, res, next) => {
  Revive.findById(req.params.id, (err, foundRevive) => {
    if(err) {
      console.log(err);
    } else {
      res.render('revive/show_revive', {revive: foundRevive});
    }
  });
};

That works just fine but then to get the author's data in the revive show view as well I tried this:

exports.showRevive = (req, res, next) => {
  Revive.findById(req.params.id)
  .populate('author')
  .exec(function(err, foundRevive) {
    if(err) {
      console.log(err);
    } else {
      res.render('revive/show_revive', {revive: foundRevive});
    }
  });
};

That did not work... Can you guys please point me in the right direction? Thanks!

SakoBu
  • 3,972
  • 1
  • 16
  • 33
  • what is in req.params.id( object Id ?) – Alok Deshwal Dec 01 '16 at 19:15
  • Is the response empty? Can you see what's inside `foundRevive` ? – drinchev Dec 01 '16 at 19:44
  • Before res.render I tried to console.log(foundRevive) - nothing it just crashes before logging with this message: message: 'Cast to ObjectId failed for value "{ id: 583e28e562c0022f1d3e1b87 }" at path "_id"', name: 'CastError', kind: 'ObjectId', value: { id: 583e28e562c0022f1d3e1b87 }, path: '_id', reason: undefined } – SakoBu Dec 01 '16 at 19:49
  • You should've put that in your question :) Read more here ... http://stackoverflow.com/a/14941216/1282674 – drinchev Dec 01 '16 at 19:55
  • double check , is it an objectId or just string – Alok Deshwal Dec 01 '16 at 20:03

0 Answers0