Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Delay in json data - Node, Express, MongoDB

I'm fairly new to nodejs and I'm doing a full stack developer challenge from devchallenges.io (Shoppingify). Below, I'm trying to add a new item. However, there's a slight delay between the return value from the request and the actual value in the database. The value updates straight away which is great however, the return value in the request is the previous value rather than being the current quantity value in the database.

// @route    POST api/category
// @desc     Add category and items
// @access   Private
router.post(
  '/',
  [
    check('name', 'Name is required').notEmpty(),
    check('category', 'Category is required').notEmpty(),
  ],
  auth,
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({
        errors: errors.array(),
      });
    }

    const { name, note, image, category } = req.body;

    const itemObject = { name, note, image, category };

    try {
      const categoryItem = await Category.find({
        user: req.user.id,
      });
      //   check if category object are empty
      if (categoryItem.length === 0) {
        const newCat = new Category({
          user: req.user.id,
          name: category,
          items: itemObject,
        });
        await newCat.save();
        res.json(categoryItem);
      } else if (categoryItem.length !== 0) {
        //   check if category name already exists
        categoryItem.map(async (cat) => {
          if (cat.name.toLowerCase() === category.toLowerCase()) {
            cat.items.push(itemObject);
            await cat.save();
            res.json(categoryItem);
          } else {
            //   create new category
            const newCat = new Category({
              user: req.user.id,
              name: category,
              items: itemObject,
            });
            await newCat.save();
            res.json(categoryItem);
          }
        });
      }
    } catch (error) {
      console.error(error.message);
      res.status(500).send('Server Error');
    }
  }
);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not returning the correct item…

Return the result of newcat.save()

Or try a new findById if newCat is not the correct object to return

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!