const user = require('../modules/User') const express = require('express') const router = express.Router(); router.get('/',(req,res)=>{ console.log(req.body) user = User(req.body); User.save(); res.send(req.body); }
const mongoose = require('mongoose') const { Schema } = mongoose;
const UserSchema = new Schema({ name: { type: String, require: true, }, email: { type: String, require: true, unique: true, }, password: { type: String, require: true, }, date: { type: Date, default: Date.now, }, });
module.export = mongoose.model('user',UserSchema)
The error simply means that the User is not defined.Because you are importing it as const user = require('../modules/User');
.You should import it as const User = require('../modules/User');
and there are other issues as well in your code.
User
not user
in mongoose.model() function , like module.exports = mongoose.model('User',UserSchema);
module.exports.User = mongoose.model('User', UserSchema);
router.get
handler .So it will be something like thisconst User = require('../modules/User');
const express = require('express');
const router = express.Router();
router.get('/', async (req, res) => {
console.log(req.body);
const user = await User(req.body);
//Now you don't need to save the user it will be saved automatically
//User.save();
res.send(user);
};