Home > Net >  mongodb is not saving all data, its only generating _id & _v in documents and node warnings
mongodb is not saving all data, its only generating _id & _v in documents and node warnings

Time:01-17

I am new to this and trying to create an rest Api using NodeJS, express and mongoose. after hitting postman, I only got id and version number but no other information.

i wrote this in postman body>raw with application/json { "name": "john cina", "username": "cina", "password": "abc123" }

and may be after installing bcrypt, when I started my server its giving so many warnings like below:

(node:2400) Warning: Accessing non-existent property 'Symbol(Symbol.toStringTag)' of module exports inside circular dependency (Use node --trace-warnings ... to show where the warning was created) s inside (node:2400) Warning: Accessing non-existent property 'instanceOfSchema' of module exports inside circular dependency (node:2400) Warning: Accessing non-existent property '_id' of module exports inside circular depenircular ddency (node:2400) Warning: Accessing non-existent property '_id' of module exports inside circular dependency dency dency app listening at port 3000 connection successfull

my codes are below:

server file.

const express = require("express");
const mongoose = require("mongoose");
const userHandler = require("./routeHandler/userHandler");


const app = express();
app.use(express.json());

//connection
mongoose.set("strictQuery", true);

mongoose
  .connect("mongodb://0.0.0.0/files", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => console.log("connection successfull"))
  .catch((err) => console.log(err));

//application routes
app.use("/user", userHandler);


function errorHandler(err, req, res, next) {
  if (res.headerSent) {
    return next(err);
  }
  res.status(500).json({ error: err });
}

app.listen(3000, () => {
  console.log("app listening at port 3000");
});

schema file.

const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  username: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  status: {
    type: String,
    enum: ["active", "inactive"],
  },
});

module.exports = userSchema;

userApifile.

const express = require("express");
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const router = express.Router();
const userSchema = require("../routeHandler/userHandler");
const User = new mongoose.model("User", userSchema);

router.post("/signup", async (req, res) => {
  try {
    const hashedPassword = await bcrypt.hash(req.body.password, 10);
    const newUser = new User({
      name: req.body.name,
      username: req.body.username,
      password: hashedPassword,
    });
    await newUser.save();
    res.status(200).json({
      message: "SIGN UP DONE",
    });
  } catch {
    res.status(500).json({
      message: "FAILED!",
    });
  }
});

module.exports = router;

CodePudding user response:

Declare your model and export that in your schema file:

const User = new mongoose.model("User", userSchema);
module.exports = User;

Then, import it with:

const User = require('../path/to/user-schema.js);
  • Related