0

who can explain with example how to get from another schema user data (for example useravatar) while i read about refs and i cant understand.

This is my code, but i want to send back not only article but with profile datas about author. How can i do this ? I have authorID already for this.

router.post('/get-article', (req, res) => {
    const { id, authorID } = req.body.data;
    Article.findByIdAndUpdate({ _id: id }, { $inc: { "pageview": 1 } }, (err, article) => {
            if (err) return res.status(400).json({ NotFound: "Article Not Found" })
            res.json({ article })
        })
})

article schema

const schema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    title: { type: String, required: true },
    image: { type: String, required: true },
    content: { type: String, required: true },
    email: { type: String, required: true },
    author: { type: String, required: true, index: true },
    added: { type: Date, default: Date.now },
    pageview: { type: Number, default: 0 }
});

User schema

const schema = new mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  username: { type: String, required: true },
  email: { type: String, required: true },
  facebookId: { type: String },
  githubId: { type: String },
  googleId: { type: String },
  useravatar: { type: String, required: true },
  userip: { type: String, required: true },
  accessToken: { type: String },
  date: { type: Date, default: Date.now }
});

0 Answers0