Search code examples
node.jsreactjsmongodbexpressmongoose-schema

Data not saved according to Schema in mongoDB


I am trying to make an inventory management app and I created a schema for mongoDB through mongoose that looks like this

  const mongoose = require("mongoose");

const Product = mongoose.model(
    "Product",
    new mongoose.Schema({  
      id: Number,
      barcode: String,
      ProductName: String,
      Price: Number,
      Quantity: Number    
       })
    );

    module.exports = Product;

when I try to save it through a React form it sends the data according to the schema. And the console log looks like this

Console

but the data is not entirely saved only Id and barcode are saved but the other elements are not saved. the data looks like this data that has been saved as

I think there is a problem with my server.js that is like this

    const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const dbConfig = require("./auth/config/db.config");

const app = express();
const { verifySignUp, authJwt } = require("./auth/middlewares");
const authController = require("./auth/controllers/auth.controller");
const userController = require("./auth/controllers/user.controller")


var corsOptions = {
  origin: "http://localhost:3000"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(bodyParser.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

const db = require("./auth/models");

db.mongoose
  .connect(`mongodb://${dbConfig.HOST}:${dbConfig.PORT}/${dbConfig.DB}`, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then(() => {
    console.log("Successfully connect to MongoDB.");

  })
  .catch(err => {
    console.error("Connection error", err);
    process.exit();
  });

// simple route
app.get("/", (req, res) => {
  res.json({ message: "cnct auth success!" });
});

// routes
require('./auth/routes/auth.routes')(app);
require('./auth/routes/user.routes')(app);
require('./InventoryManager/routes/store.routes')(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

Can anyone help me with this I am really trying to solve this for 2 days, The help would be appreciated. If anything else required to take look at please ask and help. Thank YOU in Advance


Solution

  • The object you want to save

          id: ,
          barcode: ,
          productName: ,
          price: ,
          quantity: 
    

    The Mongoose schema is different:

          id: Number,
          barcode: String,
          ProductName: String,
          Price: Number,
          Quantity: Number  
    

    ProductName, Price, Quantity don't present in your object (case matters), and these 3 fields aren't saved in your database.

    You need to change your object according to your schema. It should be :

          id: 123,
          barcode: "barcode",
          ProductName: "name",
          Price: 100,
          Quantity: 2
    

    (values are just example)