Reputation: 352
I want to save some data in mongodb database. So install mongoose node package to help me. And fallow the tutorial to do my best. But I got bellow error:
How to solve TypeError: person.save is not a function
- what is wrong?
Person Model:
const mongoose = require('mongoose');
const personSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true,
minLength: 3
}
});
const Person = mongoose.model('person', personSchema);
module.exports = Person;
Sever.js
const express = require('express');
const mongoose = require('mongoose')
let Person = require('./model/Person')
mongoose.connect(process.env.DATABAS_URL,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => {
console.log('DB Ok!')
})
const app = express();
app.use(express.json());
app.post('/add', async (req, res) => {
const person = new Person({ name: req.body.name })
console.log('/add route');
console.log(req.body);
try {
console.log(`try`);
const newPerson = await person.seve();
res.json(newPerson);
console.log(`end try: ${newPerson}`)
} catch (err) {
res.status(500).json({"err" : err});
console.log(`catch: ${err}`);
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server run on port ${PORT}`));
and here is my console:
Server run on port 3000
DB Ok!
/add route
{ name: 'Mohsen Rasouli'}
try
catch: TypeError: person.seve is not a function
and Postman App return:
{
"err": {}
}
so... I don't now where is the problem and how can I solve it? I'm new with express and mongoose.
Upvotes: 0
Views: 101