MongoDB unable to push to array when grabbing schema by _id - javascript

I have been trying to add a subscription feature to my website where the person subscribes and that class is added to their class array in the users model... Here is my code:
app.post("/subscribe", function (req, res) {
const newClass = req.body.subClass;
const id = req.user.id
const ObjectId = mongoose.Types.ObjectId;
User.findOneAndUpdate(
{ _id: new ObjectId(id) },
{ $push: { classes: newClass } },
{ upsert: false, new: true }
);
res.redirect("/dashboard")
});
Here is my schema:
const userSchema = new mongoose.Schema({
email: String,
password: String,
secret: String,
classes: [String]
});
and the model I am pushing to:
const User = new mongoose.model("User", userSchema)
All help is appreciated

app.post("/subscribe", async function (req, res) {
try {
const newClass = req.body.subClass;
const id = req.user.id
const ObjectId = mongoose.Types.ObjectId;
await User.findByIdAndUpdate(
id,
{ $push: { classes: newClass } },
{upsert: true, new: true }
);
}
catch(err) {
console.error(err);
}
res.redirect("/dashboard")
});
Here's mongoose references link

Related

Getting Empty Object during Put request to the server

I created an API for following and followers user for Social Media Application, while request from postman getting empty object: {} But it seems to me to be correct.
Model:
const mongoose = require("mongoose");
const UserSchema = mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
firstname: {
type: String,
required: true,
},
lastname: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
profilePicture: String,
coverPicture: String,
about: String,
livesin: String,
workAt: String,
relationship: String,
followers: [],
following: []
},{
timestamps:true
}
);
const UserModel = mongoose.model("Users", UserSchema);
module.exports = UserModel;
UserControler:
const UserModel = require("../Models/Usermodel");
const bcrypt = require("bcryptjs");
const followUser = async (req, res) => {
const id = req.params.id.trim();
const { currentUserId } = req.body;
if (currentUserId === id) {
res.status(403).send("Action forbiden");
} else {
try {
const followUser = await UserModel.findById(id);
const followingUser = await UserModel.findById(currentUserId);
if (!followUser.followers.includes(currentUserId)) {
await followUser.updateOne({ $push: { followers: currentUserId } });
await followingUser.updateOne({ $push: { following: id } });
res.status(200).send({message:"User Followed"});
} else {
res.status(403).send("User alredy followed by you!");
}
} catch (error) {
res.status(500).send(error);
}
}
};
module.exports = { getUser, updateUser, userDelete, followUser };
UserRoute:
const express = require("express");
const {getUser,updateUser, userDelete, followUser} = require("../Controller/userControler");
const router = express.Router()
router.get("/:id",getUser)
router.put("/:id",updateUser)
router.delete("/:id", userDelete)
router.put("/:id/follow", followUser)
module.exports=router;
index.js:
app.use("/user",UserRoute)
Here is the complete details regarding the error, let me know what happens in the code, thank you.
i assume that you have all the other functions other than followUser in your controller.js
The thing is that you must first specify the field name on the basis of which you want to update the document.
Here is what you need to do;
const UserModel = require("../Models/Usermodel");
const bcrypt = require("bcryptjs");
const mongoose = require("mongoose");//updated line
const followUser = async (req, res) => {
const id = req.params.id.trim();
const { currentUserId } = req.body;
if (currentUserId === id) {
res.status(403).send("Action forbiden");
} else {
try {
const followUser = await UserModel.findById({_id: mongoose.Types.ObjectId(id)});
const followingUser = await UserModel.findById({_id: mongoose.Types.ObjectId(currentUserId)});
if (!followUser.followers.includes(currentUserId)) {
await followUser.updateOne({_id: mongoose.Types.ObjectId(*id of the user you want to update*)},{ $push: { followers: currentUserId } });
await followingUser.updateOne({_id: mongoose.Types.ObjectId(*id of the user you want to update*)}{ $push: { following: id } });
res.status(200).send({message:"User Followed"});
} else {
res.status(403).send("User alredy followed by you!");
}
} catch (error) {
res.status(500).send(error);
}
}
};
module.exports = { getUser, updateUser, userDelete, followUser };
And while hitting the api pls make sure that your route should be
localhost:port-number/user/12345789/follow
and also make sure that the API type in postman must be same as in the backend e.g; PUT
please try findByIdAndUpdate query insted of using updateOne

How does a function writer call the getByName . function

books-controllers
I want the data to appear by the name in the postman and not the ID because I have information and I want to fetch it through the name in the database
const getByName = async (req, res, next) => {
const name = req.params.name;
let book;
try {
book = await Book.getByName("name");
} catch (err) {
console.log(err);
}
if (!book)
return res.status(404).json({ message: "No book found" });
}
return res.status(200).json({ book });
};
modelSchema
Here is the Skyma model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bookSchema = new Schema({
name: {
type: String,
require: true
},
description: {
type: String,
require: true
},
price: {
type: Number,
require: true
},
avilable: {
type: Boolean,
},
image: {
type: String,
require: true
},
});
module.exports = mongoose.model("Book", bookSchema);
There in no in-built method in mongoose getByName. You can use generic find to search for object using name
let book = await Book.find({ name: name }).exec();
You can also use findOne if needed.
You can try this -
async function getByName(req, res){
const bookname = req.params.name ;
try {
const book = await Book.findOne({name: bookname})
if(book!==null) {
res.status(200).send({'data': book}) ;
}
else {
res.status(404).send("No book found !")
}
}
catch(error) {
console.log(error)
res.send("Error")
}
}

Delete nested object with mongoose

I got this mongoose schemas:
const UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true,
},
groups: [
{
groupName: {
type: String,
required: true,
},
groupMembers: [{ type: Schema.Types.ObjectId, ref: "GroupMember" }],
},
],
});
const GroupMemberSchema = new Schema({
firstName: String,
lastName: String,
birthday: Date,
gender: String,
age: Number
});
I want to have 2 routes:
Delete a group member from the groupMembers array based on objectId.
Delete a whole group from the groups array based on objectId.
My attempt for the delete group member route. This route removes the group member from the groupMembers collection succesfully, but the group member still exist in the user collection:
router.delete(
"/:userId/:groupId/:groupMemberId",
catchAsync(async (req, res) => {
const { userId, groupId, groupMemberId } = req.params;
await GroupMember.findByIdAndDelete(groupMemberId);
const user = await User.findById(userId);
const group = user.groups.find((group) => group._id.toString() === groupId);
const groupIndex = user.groups.indexOf(group);
const updatedGroupmembers = user.groups[groupIndex].groupMembers.filter(groupMember=> groupMember._id.toString()!==groupMemberId);
res.send({updatedGroupmembers})
})
);
Did you try using $pull?
router.delete(
'/:userId/:groupId/:groupMemberId',
catchAsync(async (req, res) => {
const { userId, groupId, groupMemberId } = req.params;
await GroupMember.findByIdAndDelete(groupMemberId);
const updatedUser = await User.findByIdAndUpdate({ id: userId }, { $pull: { groups: groupId } }, { new: true });
res.send({ updatedUser });
}),
);

Can't pass multiple documents in an array when using findById

I want to find each of the elements in the array by their id and send it with the post request to create a new post. Right now when I create a new post only passes the first index of the array and if I use find() it passes all of the social schemas regardless if it is in the body of the request. I hope this makes sense if it doesn't please let me know. I hope someone can help.
Below is the mongoose schema for the qrcode post also using Joi
const Joi = require("joi");
const mongoose = require("mongoose");
const { themeSchema } = require("./Theme");
const { userSchema } = require("./User");
const { socialSchema } = require("./Social");
const QrCode = mongoose.model(
"QrCode",
new mongoose.Schema({
user: {
type: userSchema,
required: true,
},
name: {
type: String,
maxLength: 255,
required: true,
trim: true,
},
theme: {
type: themeSchema,
required: true,
},
// Social Media Links
social: [
{
type: socialSchema,
required: true,
},
],
})
);
function ValidateQrCode(qrCode) {
const schema = {
userId: Joi.objectId(),
name: Joi.string().max(255).required(),
themeId: Joi.objectId().required(),
socialId: Joi.array().required(),
};
return Joi.validate(qrCode, schema);
}
module.exports.QrCode = QrCode;
module.exports.validate = ValidateQrCode;
this is the post route to create a new qrcode
router.post("/", auth, async (req, res) => {
const { error } = validate(req.body);
if (error) res.status(400).send(error.details[0].message);
const theme = await Theme.findById(req.body.themeId);
if (!theme) return res.status(400).send("Invalid theme.");
const user = await User.findById(req.user._id);
if (!user) return res.status(400).send("Invalid theme.");
const social = await Social.findById(req.body.socialId);
if (!social) return res.status(400).send("Invalid social.");
const qrCode = new QrCode({
user: user,
name: req.body.name,
theme: theme,
social: social,
});
await qrCode.save();
res.send(qrCode);
});
In the body of my Postman request I am inputting the info below
{
"name": "Friends",
"themeId": "60f89e0c659ff827ddcce384",
"socialId": [
"60f89e43659ff827ddcce386",
"60f89e5c659ff827ddcce388"
]
}
To fetch data using ids, you can use below mongodb query simply,
db.collection.find( { _id : { $in : ["1", "2"] } } );
In mongoose,
model.find({
'_id': { $in: [
mongoose.Types.ObjectId('1'),
mongoose.Types.ObjectId('2'),
mongoose.Types.ObjectId('3')
]}
}, function(err, docs){
console.log(docs);
});
Or
await Model.find({ '_id': { $in: ids } });

Mongoose populate array is empty

I have defined a UserSchema and a PostSchema in my model.js. UserSchema has a ref to its own Posts, while PostSchema has a ref to its author.
In my controllers.js methods I'm creating a user and a post instance (bound to the newly created user). I'm now trying to populate the user's posts, but mongoose returns an empty array. However, the other way around works (I'm able to retrieve the user by a Post instance).
models.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: String,
posts: [{
type: Schema.Types.ObjectId,
ref: 'Post'
}]
})
const PostSchema = new Schema({
content: String,
author: {
type: Schema.Types.ObjectId,
ref: 'User'
}
})
const Post = mongoose.model('Post', PostSchema, 'posts');
const User = mongoose.model('User', UserSchema, 'users');
module.exports = { Post, User }
controllers.js
require('../model/db'); // DB config
const mongoose = require('mongoose');
const models = require('../model/models');
const getUser = (req, res) => {
const user = new models.User({
_id: new mongoose.Types.ObjectId(),
username: 'JohnDoe'
});
user.save(function (err) {
if (err) return res.json(err);
const post = new models.Post({
content: 'New Post',
author: user.id
});
post.save(function (err) {
if (err) return res.json(err);
models.User.findOne({
username: 'JohnDoe'
})
.populate('posts').exec((err, user) => {
res.json(user);
})
});
});
}
const getPost = (req, res) => {
const user = new models.User({
_id: new mongoose.Types.ObjectId(),
username: 'JohnDoe'
});
user.save(function (err, user) {
if (err) return res.json(err);
const post = new models.Post({
content: 'NewPost',
author: user.id
});
post.save(function (err, post) {
if (err) return res.json(err);
models.Post.findOne({
content: 'NewPost'
})
.populate({
path: 'author',
model: 'User'
}).exec((err, user) => {
res.json(user);
})
});
});
}
getUser's result (posts is empty):
{
"posts": [],
"_id": "5b9426a6fd187d3949470f54",
"username": "JohnDoe",
"__v": 0
}
getPost result (author is correctly returned)
{
"_id": "5b94287e99072c3a173419f2",
"content": "NewPost",
"author": {
"posts": [],
"_id": "5b94287e99072c3a173419f0",
"username": "JohnDoe",
"__v": 0
},
"__v": 0
}
You need to push the post id to the posts array in the user model as follows
const getUser = (req, res) => {
const newUser = new models.User({ username: 'JohnDoe' });
newUser.save(user => {
if (err) return res.json(err);
const newPost = new models.Post({
content: 'New Post',
author: user._id
});
newPost.save(post => {
if (err) return res.json(err);
models.User.findByIdAndUpdate(user._id,
{ '$push': { 'posts': post._id } },
{ 'new': true }
)
.populate('posts')
.exec((err, u) => {
res.json(u);
})
});
});
}
Using async/await
const getUser = async (req, res) => {
try {
const newUser = new models.User({ username: 'JohnDoe' });
const user = await newUser.save();
const newPost = new models.Post({
content: 'New Post',
author: user._id
});
const post = await newPost.save();
const result = await models.User.findByIdAndUpdate(user._id,
{ '$push': { 'posts': post._id } },
{ 'new': true }
)
.populate('posts')
.exec();
res.json(result)
}
catch (err) {
return res.json(err);
}
}

Categories

Resources