I'm trying to implement the favoriting feature following a tutorial, but I'm encountering issues with making it work. Any assistance would be greatly appreciated. Thank you!
UserSchema:
var UserSchema = new mongoose.Schema({
username: {type: String, lowercase: true, unique: true, required: [true, "cannot be blank"], match: [/^[a-zA-Z0-9_]+$/, 'is invalid'], index: true},
email: {type: String, lowercase: true, unique: true, required: [true, "cannot be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true},
bio: String,
image: String,
hash: String,
salt: String,
following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Article' }]
}, {timestamps: true});
User favorite article method:
UserSchema.methods.favorite = function(id){
if(this.favorites.indexOf(id) === -1){
console.log("id is: ", id) //
this.favorites.concat(id);
}
console.log("this favorites is:", this.favorites);
return this.save();
};
Post request to mark an article as a favorite:
router.post('/:article/favorite', auth.required, function(req, res, next) {
var articleId = req.article._id;
User.findById(req.payload.id).then(function(user){
if (!user) { return res.sendStatus(401); }
return user.favorite(articleId).then(function(){
return req.article.updateFavoriteCount().then(function(article){
return res.json({article: article.toJSONFor(user)});
});
});
}).catch(next);
});
Curl request example:
curl --location --request POST 'http://localhost:3000/api/articles/how-to-train-your-dragon-m06dim/favorite' \
--header 'Content-Type: application/json' \
--header 'X-Requested-With: XMLHttpRequest' \
--header 'Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlOTIxMTQ3ZWYxZjkxMzcwZjEwMDkwNiIsInVzZXJuYW1lIjoidGl5YXl1IiwiZXhwIjoxNTkyMDIzODMxLCJpYXQiOjE1ODY4Mzk4MzF9.pIA7RVgVbRI6-2IQzW2vptEzwiZrqCroz8-SdGRNEF8' \
--data-raw ''
In the server logs, it shows that the user's favorites list remains empty even after marking an article as a favorite:
Listening on port 3000
Mongoose: users.ensureIndex({ username: 1 }) { unique: true, background: true }
Mongoose: articles.ensureIndex({ slug: 1 }) { unique: true, background: true }
Mongoose: users.ensureIndex({ email: 1 }) { unique: true, background: true }
Mongoose: articles.findOne({ slug: 'how-to-train-your-dragon-m06dim' }) { fields: undefined }
Mongoose: users.find({ _id: { '$in': [ { inspect: [Function: inspect] } ] }}) { fields: undefined }
Mongoose: users.findOne({ _id: { inspect: [Function: inspect] } }) { fields: undefined }
id is: ObjectID { _bsontype: 'ObjectID', id: '^•C\u0006’ï`w2%\u0014f' }
this favorites is: []
Mongoose: users.count({ '$and': [ { username: 'tiyayu' }, { _id: { '$ne': { inspect: [Function: inspect] } } } ]}) {}
Mongoose: users.count({ '$and': [ { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="55213c2c342c20152c342c347b363a38">[email protected]</a>' }, { _id: { '$ne': { inspect: [Function: inspect] } } } ]}) {}
Mongoose: users.update({ _id: { inspect: [Function: inspect] } }) { '$set': { updatedAt: { inspect: [Function: inspect] } } }
Mongoose: users.count({ favorites: { '$in': [ { inspect: [Function: inspect] } ] }}) {}
Mongoose: articles.count({ '$and': [ { slug: 'how-to-train-your-dragon-m06dim' }, { _id: { '$ne': { inspect: [Function: inspect] } } } ]}) {}
Mongoose: articles.update({ _id: { inspect: [Function: inspect] } }) { '$set': { updatedAt: { inspect: [Function: inspect] } } }
POST /api/articles/how-to-train-your-dragon-m06dim/favorite 200 126.823 ms - 423