Mongoose is not saving array in the mongo db - node.js

tasks array is not getting saving the data base when I'm sending data from postman.
I'm sending data by hitting postman but it is only saying the title field values but not tasks array values like below
{
"title": "my first test59",
"tasks": ["task1","task2","task3"]
}
Since I'm using create method save is not needed that's why I'm not using it in my model. I'm not getting it why it is not working out.
My model:
const mongoose = require("mongoose");
const schema = mongoose.Schema;
const toDoSchema = new schema({
title:{
type: String,
require: [true, "title is required"],
trim: true
},
tasks:{
type:[String],
trim: true
}
});
const toDoModel = mongoose.model("toDo", toDoSchema);
module.exports = toDoModel;
//importing model
const toDoModel = require('../models/ToDoModel');
//creating controller function
const createToDoController = async (req,res)=>{
try{
const {title,toDoTasks} = req.body;
if(!title) //input validation
{
throw new Error("Please provide the title");
}
const newToDo = await toDoModel.create({ title, toDoTasks });
res.status(201).json({
success: true,
message: "User Created Successfully",
newToDo,
});
}
catch(err){
res.status(500).json({
success: false,
message: "Unable to perform create operation",
error: err
})
res.send(err.message);
}
}
module.exports = createToDoController;
controller.js
//importing model
const toDoModel = require('../models/ToDoModel');
//creating controller function
const createToDoController = async (req,res)=>{
try{
const {title,toDoTasks} = req.body;
if(!title) //input validation
{
throw new Error("Please provide the title");
}
const newToDo = await toDoModel.create({ title, toDoTasks });
res.status(201).json({
success: true,
message: "User Created Successfully",
newToDo,
});
}
catch(err){
res.status(500).json({
success: false,
message: "Unable to perform create operation",
error: err
})
res.send(err.message);
}
}
module.exports = createToDoController;

Change "tasks" instead of "toDoTasks" in request body. It will work.
const {title,tasks} = req.body;

Related

MERN STACK image upload to MongoDB

hi can someone help me out im developing a MERN STACK APP with aws s3 i have 2 schema models one for user and one for images, im making a ref to the user inside my image schema, im successfully adding to MongoDB atlas the user with image, my issue is i want the user to be able to add the image in the existing array not a new one like its doing now and also how can i get all the data for the user thats logged in on the front end as off now im getting the same images for all users who log in.
-image schema-
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const imageSchema = new Schema(
{
user: {
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
image: {
type: [String],
default: "",
required: true,
},
},
{
timestamps: true,
}
);
module.exports = mongoose.model("PostImage", imageSchema);
-upload image-
const imageUploadS3 = (req, res, next) => {
const uploadSingle = upload("mern-stack-imageupload-insta").single("image");
uploadSingle(req, res, async (err) => {
if (err)
return res.status(400).json({ success: false, message: err.message });
console.log(req.file);
const imgDB = await PostImage.create({
user: req.userId,
image: req.file.location,
});
try {
await imgDB.save();
res.status(200).json({
success: true,
message: "image uploaded",
image: imgDB,
data: req.file.location,
});
} catch (error) {
res.status(500).json({ message: error.message });
}
});
};
getImage route
const getImages = async (req, res) => {
const { id } = req.userId;
try {
const image = await PostImage.findById(id);
if (!image) {
return res.status(404).json({ message: "Image not found" });
}
res.status(200).json(image);
} catch (error) {
res.status(409).json({ message: error.message });
}
};

UnhandledPromiseRejectionWarning: ReferenceError: book is not defined

I stuck with a problem, when I hit my route addBook then getting an error like book is not defined don't know where I am please try to fix my code. if you have any query on my code please let me know.
route.js
This is the route.js file where I wrote my all logics
const express = require("express");
const router = express.Router();
const Publisher = require('../model/Categary');
const Book = require('../model/Product');
// const {addCatogary} = require('../controllers/Product');
// router.get('/addcatogary',addCatogary);
router.post("/addPublisher", async (req, res) => {
try {
//validate req.body data before saving
const publisher = new Publisher(req.body);
await publisher.save();
res.status(201).json({ success: true, data: publisher });
console.log(publisher);
} catch (err) {
res.status(400).json({ success: false, message: err.message });
}
console.log(err);
});
router.post("/addBook", async (req, res) => {
try {
//validate data as required
const book = new Book(req.body);
// book.publisher = publisher._id; <=== Assign user id from signed in publisher to publisher key
await book.save();
const publisher = await Publisher.findById({ _id: book.publisher });
publisher.publishedBooks.push(book);
await publisher.save();
//return new book object, after saving it to Publisher
res.status(200).json({ success: true, data: book });
} catch (err) {
res.status(400).json({ success: false, message: err.message });
}
console.log(book);
});
router.get("/publishers", async (req, res) => {
try {
const data = await Publisher.find().populate({
path: "booksPublished",
select: "name publishYear author",
});
res.status(200).json({ success: true, data });
} catch (err) {
res.status(400).json({ success: false, message: err.message });
}
console.log(data)
});
module.exports = router;
Product.js
const mongoose= require('mongoose');
const {Schema} = require('mongoose');
const bookSchema = new Schema({
name: String,
publishYear: Number,
author: String,
publisher: {
type: Schema.Types.ObjectId,
ref: 'Publisher',
required: true
}
},
{timestamps: true});
module.exports = mongoose.model('Book', bookSchema);
Catogary.js
This is the Catogary model.
const mongoose = require('mongoose');
const {Schema} = require('mongoose');
const publisherSchema = new Schema({
name: String,
location: String
},
{timestamps: true}
);
publisherSchema.virtual('booksPublished', {
ref: 'Book', //The Model to use
localField: '_id', //Find in Model, where localField
foreignField: 'publisher', // is equal to foreignField
});
// Set Object and Json property to true. Default is set to false
publisherSchema.set('toObject', { virtuals: true });
publisherSchema.set('toJSON', { virtuals: true });
module.exports = mongoose.model('Publisher', publisherSchema);
router.post("/addBook", async (req, res) => {
try {
//validate data as required
const book = new Book(req.body);
// book.publisher = publisher._id; <=== Assign user id from signed in publisher to publisher key
await book.save();
const publisher = await Publisher.findById({ _id: book.publisher });
publisher.publishedBooks.push(book);
await publisher.save();
//return new book object, after saving it to Publisher
res.status(200).json({ success: true, data: book });
} catch (err) {
res.status(400).json({ success: false, message: err.message });
}
console.log(book);
});
hey brother!
you have written a nice code but you have made a small mistake after catch block you are logging book which is defined inside try block and that book variable can not be accessed outside of that box because its local variable and can be only used inside try block
if you wanted to used that variable outside of try{} catch(){} block defined it with var
just check below link
geeksforgeeks.org/global-and-local-variables-in-javascript
happy hacking :)

Update items quantity in shopping cart in MongoDB (MERN Stack)

I have a function to add items to the shopping cart. Inside the function I have an if-else case. If the cart does not exist for that given user then it is created from scratch, otherwise (else case) if the cart already exists for that given user, then the key quantity must be updated to the specific itemId of that given item with the same Id.
In the function there is already the code block of the if case in case the cart does not exist. What can I do if I need to update the quantity of the Items?
Cart Schema
const mongoose = require('mongoose');
const idValidator = require('mongoose-id-validator');
const Schema = mongoose.Schema;
const cartItemSchema = new Schema ({
quantity: { type: Number, required: true },
itemId: { type: mongoose.Types.ObjectId, required: true, ref: 'Meal' }
});
const cartSchema = new Schema ({
cartItems : [
cartItemSchema
],
customer: { type: mongoose.Types.ObjectId, required: true, unique: true, ref: 'User',}
});
cartSchema.plugin(idValidator);
module.exports = mongoose.model('Cart', cartSchema);
Function
const addToCartByUserId = async (req, res, next) => {
const userId = req.params.uid;
const errors = validationResult(req);
if (!errors.isEmpty()) {
return next(
new HttpError('Invalid inputs passed, check your data.', 422)
);
}
/* Find user in Database */
let user;
try {
user = await User.findById(userId);
} catch(err) {
const error = new HttpError('Creating cart failed, try again later.', 500);
return next(error);
}
if (!user) {
const error = new HttpError('Could not find user for provided id.', 404);
return next(error);
}
/* getCartByUserId */
let cart;
try {
cart = await Cart.find({ customer: userId });
} catch(err) {
const error = new HttpError('Something went wrong, could not find an user by its id.', 500);
return next(error);
}
// if cart doesn't exists. It works
if (!cart || cart.length === 0) {
const { cartItems } = req.body;
const createdCart = new Cart ({
cartItems,
customer: userId
});
try {
const session = await mongoose.startSession();
await session.withTransaction(async () => {
createdCart.save({ session });
});
session.endSession();
} catch(err) {
const error = new HttpError(
'Creating cart failed, please try again.',
500
);
console.log(err);
return next(error);
}
res.status(201).json({ cart: createdCart });
} else { // if cart already exists
let quantityToInsert = cart.cartItems[cartItems.itemId].quantity + cartItems.quantity;
cart.cartItems.push({
quantity: quantityToInsert
});
}
};

How to Delete document and sub documents referenced from others collections - MongoDB Mongoose

I have this collection Cart (cart schema) to delete and it is referenced with 2 other schemes, Meal and Customer (owner user, its schema is: User Schema).
How can I delete the cart by passing as req.params.id the user's id from the HTTP request?
Cart Schema
const mongoose = require('mongoose');
const idValidator = require('mongoose-id-validator');
const Schema = mongoose.Schema;
const cartItemSchema = new Schema ({
quantity: { type: Number, required: true },
itemId: { type: mongoose.Types.ObjectId, required: true, ref: 'Meal' }
});
const cartSchema = new Schema ({
cartItems : [
cartItemSchema
],
customer: { type: mongoose.Types.ObjectId, required: true, ref: 'User'}
});
cartSchema.plugin(idValidator);
module.exports = mongoose.model('Cart', cartSchema);
I created a function to delete the document, but it doesn't work, it returns the message: 'Deleted cart.', but isn't true, the document remains in collection.
const deleteCartByUserId = async (req, res, next) => {
const userId = req.params.uid;
let cart;
try {
cart = await Cart.find({ customer: userId });
} catch(err) {
const error = new HttpError('Something went wrong, could not delete cart.', 500);
return next(error);
}
if(!cart) {
const error = new HttpError('Could not find cart for this user id.', 404);
return next(error);
}
try {
Cart.deleteOne({ customer: userId });
} catch(err) {
console.log(err);
const error = new HttpError('Something went wrong, could not delete cart.', 500);
return next(error);
}
res.status(200).json({ message: 'Deleted cart.' });
};
So the porblem was that you missed an await before delete one function call.
Also I've changed some of youre code to make it cleaner:
const functionHandler = fn =>
(req, res, next) =>
Promise
.resolve(fn(req, res, next))
.catch(next);
const deleteCartByUserId = functionHandler(async (req, res) => {
const { params: { uid: userId } } = req;
const cart = await Cart.findOneAndDelete({ customer: userId })
if(!cart) {
throw new HttpError('Could not find cart for this user id.', 404);
}
res.status(200).json({ message: 'Deleted cart.' });
});
In your error handler middleware you can check for error type and if it's not HttpError use internal server error.

CRUD for 3 different entities node js /express

I am a beginner in Node Js /Express and am currently trying to design a simple chess forum. I am having great difficulty creating the CRUD functions for the different entities. The 3 entities are: User, Message and Colour.
Their relationship to each other is User.OneToMany(Message) and Colour.OneToMany(Message).I use Mongoose.
user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var user = new Schema({
name: {
type: String
},
email: {
type: String
},
password: {
type: String
}
});
const UserDB= mongoose.model('user', user);
module.exports = UserDB;
message.js
const mongoose = require('mongoose');
const Schema= mongoose.Schema;
var message = new Schema({
titel: {
type: String
},
message: {
type: String
},
user: {
type: Schema.Types.ObjectId,
required: true,
ref: 'User'
},
color: {
type: Schema.Types.ObjectId,
required: true,
ref: 'color'
}
});
const MessageDB= mongoose.model('message', message);
module.exports=MessageDB
color.js
const mongoose = require('mongoose');
const Schema= mongoose.Schema;
var color = new Schema({
color: {
type: String
}
});
const ColorDB=mongoose.model('Color', color);
module.exports=ColorDB
I have implemented all the CRUD functions for the user.
controller.js
var UserDB = require('../model/user');
//CRUD for User
//(c)reate new User
exports.create=(req,res)=>{
//validate
if(!req.body){
res.status(400).send({message:'Empty'});
return;
}
//hash password
//const hashedPassword= bcrypt.hashSync(req.body.password,salt)
//
//new user
const user = new UserDB({
name: req.body.name,
email: req.body.email,
password: req.body.password
})
//save database
user
.save(user)
.then(data => {
res.redirect('/signup')
})
.catch(err =>{
res.status(500).send({
message: err.message || "Mars Attacks !"
})
});
}
//(r)ead User
exports.find=(req,res)=>{
//find by email (login)
if (req.query.email){
const email=req.query.email;
const password=req.query.password;
UserDB.findOne({email: email, password:password})
.then(data=>{
if(!data){
res.status(404).send({message: 'Not found user with email' +email})
} else {
res.redirect('/forum')
}
})
.catch(err=>{
res.status(500).send({message: 'Error reading user with email' +email})
})
}
//find by id
if(req.query.id){
const id=req.query.id;
UserDB.findById(id)
.then(data=>{
if(!data){
res.status(404).send({message: 'Not found user with id' +id})
} else{
res.send(data)
}
})
.catch(err=>{
res.status(500).send({message: 'Error reading user with id' +id })
})
}else{
UserDB.find()
.then(user =>{
res.send(user)
})
.catch(err =>{
res.status(500).send({
message: err.message || 'Ack! Ack!' })
})
}
}
//(u)pdate User
exports.update=(req,res)=>{
if(!req.body){
return res
.status(400)
.send({message: 'Data to update can not be empty.'})
}
const id= req.params.id;
UserDB.findByIdAndUpdate(id,req.body,{useFindAndModify: false})
.then(data=>{
if(!data){
res.status(404).send({message: `Cannot Update user with ${id}`})
}else{
res.send(data)
}
})
.catch(err=>{
res.status(500).send({message: 'Laserbeam !'})
})
}
//(d)elete User
exports.delete=(req,res)=>{
const id = req.params.id;
UserDB.findByIdAndDelete(id)
.then(data=> {
if(!data){
res.status(404).send({message: `Cannot Delete ${id}`})
}else{
res.send({
message: 'Gone!'
})
}
})
.catch(err=>{
res.status(500).send({
message:'Ack ! Could not delete User with id=' + id
});
});
}
My router looks like this:
router.js
const express = require('express');
const route = express.Router();
const controller = require ('../controller/controller');
//API
route.post('/api/users',controller.create);
route.get('/api/users',controller.find);
route.put('/api/users/:id',controller.update);
route.delete('/api/users/:id',controller.delete);
module.exports=route;
I tried to transfer the concept of the controller to the other 2 entities (message,colour). Unfortunately in vain. What are the crud functions for the other two entities ?
first you need to save the user ID as a session or via token. I would suggest look into passport.js for more information on this one.
After you have set the session.. you can access the userID in req.user.
You can now easily create a CRUD like this as an example - create only
// route
route.post('/api/message',messageController.create);
// create controller
// post data json
// {
// color:'color_ID'
// }
function create(req, res){
const message = {
titel:'this is a title',
message:'this is a message',
user: req.user,
color: req.body.color
};
await Message.create(message);
res.json({mesage:'Message Created'})
}

Resources