Skip to content

Manage store api not working #2

@kartikgx13

Description

@kartikgx13

const Store = require("../models/store");

// Add Store
const addStore = (req, res) => {
try {
console.log(req.body); // Log the request body
const { userId, name, category, address, city } = req.body;

const newStore = new Store({
  userID: userId, // Assuming the property name is userId in the request body
  name,
  category,
  address,
  city
});

newStore.save()
  .then((result) => {
    res.status(200).send(result);
  })
  .catch((err) => {
    console.error("Error saving store:", err); // Log the error
    res.status(400).send(err);
  });

} catch (err) {
console.error("Error adding store:", err); // Log the error
res.status(500).send(err);
}
};

// Get All Stores
const getAllStores = async (req, res) => {
try {
const { userID } = req.query;
console.log("UserID:", userID); // Log the userID
const findAllStores = await Store.find({ "userID": userID }).sort({ _id: -1 });
res.json(findAllStores);
} catch (err) {
console.error("Error fetching stores:", err); // Log the error
res.status(500).send(err);
}
};

module.exports = { addStore, getAllStores };
here on the console its showing UserID undefined
Data also not getting added to mongodb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions