When I try to give the id of the tag , it is still showing empty in the postman .create bookmark post route.Here is how my model and controller looksconst mongoose = require('mongoose');//bookmark modelconst bookmarkSchema = mongoose.Schema( {link: {type: String,required: [true, 'Bookmark URL or Link is required'],unique: true, },title: String,createdAt: Number,updatedAt: Number,Publisher: String,tags: [ {type: mongoose.Schema.ObjectId,ref: 'Tag', }, ], }, { // Make Mongoose use Unix time (seconds since Jan 1, 1970)timestamps: { currentTime: () => Math.floor(Date.now() / 1000) }, });const Bookmark = mongoose.model('Bookmark', bookmarkSchema);module.exports = Bookmark;//tag modelconst mongoose = require('mongoose');const tagSchema = mongoose.Schema( {title: {type: String,unique: true,},createdAt: Number,updatedAt: Number, }, {// Make Mongoose use Unix time (seconds since Jan 1, 1970)timestamps: { currentTime: () => Math.floor(Date.now() / 1000) }, });const Tag = mongoose.model('Tag', tagSchema);module.exports = Tag;Bookmark controller jsexports.createBookmark = async (req, res) => { const { link } = req.body; try { const newBookmark = await Bookmark.create({ link, }); res.status(201).json({ data: newBookmark, status: 'successfull' }); } catch (err) { console.log(err); res.status(400).json({ error: err, status: 'fail' }); }};
Submitted August 31, 2020 at 04:37AM by DVGY
No comments:
Post a Comment