I want to save doc to DataBase but when i create new User and try to save it function .save() not working
I dont have any variants to solve this problem. I tried to do everything what i found in Internet
I check some overflow posts but anyone helps me
My Index.js file:
const app = express();
const port = 5000
app.use(express.json());
app.set('view engine', 'ejs');
const uri = '<my link>';
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
}
);
client
.connect()
.then((res)=>console.log('Connected'))
.catch((error)=>console.log(error));
app.post('/auth/register',async (req, res)=> {
try {
const user = new User(req.body);
await user.save();
res.json({
success: true,
});
} catch (err) {
res.status(500)
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
My model/user.js file
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({
name: {
type:String,
required: true
},
email: {
type:String,
required: true,
index: {unique: true}
},
passwordHash: {
type:String,
required: true
},
access: {
type: Boolean,
default: 0
},
},
{
timestamps: true
});
const User = mongoose.model('User', UserSchema);
export default User