Node Express.js - can't update categories

Hi guys,
I’m working on my first e-commerce project, backend part using Express.js.
I don’t see the errors when I try to “update categories”. I tested the other functionalities (findAll, findOne, create, delete) with Postman and all is working properly.
Can you help me pls. Thanks :slight_smile:
This is my code (I share the line of code from the routes, models and controllers files):
------ categories.routes.js -------------

const router = require('express').Router();
const controller = require('../controllers/categories.controllers');

router.get('/', controller.findAll);
router.get('/:category_id', controller.findOne);
router.post('/createcat', controller.insert);
router.post('/updatecat', controller.update);
router.post('/deletecat', controller.delete);

module.exports = router;

----------- categories.models.js---------------

const mongoose = require('mongoose');

const categoriesSchema = new mongoose.Schema({
	name: { type: String, unique: true, required: true },
	image: { type: String, required: true }
});
module.exports = mongoose.model('categories', categoriesSchema);

---------- categories.controllers.js------------------

const Categories = require('../models/categories.models');

class CategoriesController {
	async findAll(req, res){
		try {
			const categories = await Categories.find({});
			res.send(categories)
		}
		catch(error){
			res.send('Something went wrong', error)
		}
	}

	async findOne(req, res){
		let { category_id } = req.params;
		try {
			const category = await Categories.findOne({_id: category_id})
			res.send(category)
		}
		catch(error){
			res.send('Something went wrong', error)
		}
	}

	async insert(req, res){
		let { name, image } = req.body;
		try {
			const category = await Categories.create({name, image});
			res.send(category)
		}
		catch(error){
			res.send('Something went wrong', error)
		}
	}

	async update(req, res){
		let { category, newCategory } = req.body;
		try {
			const updated = await Categories.updateOne( { category }, { category:newCategory } );
			res.send({updated})
		}
		catch(error){
			res.send('Something went wrong', error)
		}
	}

	async delete(req, res){
		let { category } = req.body;
		try {
			const removed = await Categories.deleteOne( { category } );
			res.send({removed});
		}
		catch(error){
			res.send('Something went wrong', error)
		}
	}
}

module.exports = new CategoriesController

Hello there,

What are you expecting to happen?

With the way it is laid out, I would expect all documents matching {category: category} to be updated to have a new category. Then, a query is returned, and sent to the requester.


Something to watch out for:

Note:
All top level keys which are not atomic operation names are treated as set operations:

Example:

const query = { name: 'borne' };
Model.update(query, { name: 'jason bourne' }, options, callback);

// is sent as
Model.update(query, { $set: { name: 'jason bourne' }}, options, function(err, res));
// if overwrite option is false. If overwrite is true, sent without the $set wrapper.

This helps prevent accidentally overwriting all documents in your collection with { name: 'jason bourne' } .

Mongoose v5.11.14: API docs (mongoosejs.com)

Hope this helps


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Just use the objectid of category see if it works

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.