0
Hello!
I am learning to use Mongoose. I am trying to create a model, but the terminal is returning the following error: "Cannot read Property 'Schema' of Undefined"
Where I’m going wrong?
Follow the codes:
User ts.
import mongoose from 'mongoose';
let Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
default: '',
trim: true
},
email: {
type: String,
default: '',
trim: true
},
password: {
type: String,
default: ''
},
register: {
type: Date
}
});
export const User = mongoose.model('user', UserSchema);
index ts.
import * as express from 'express';
import { connect } from './app/database/db';
import { User } from './app/models/User';
const server = express();
if (connect) {
console.log('Conentado à base de dados com suceso.');
server.listen(3000, () => {
console.log('Servidor rodando em http://localhost:3000');
}
);
} else {
console.log('Erro ao tentar conectar à base de dados.');
}
server.get('/user', (req, res) => {
User.find()
.then((users) => {
res.json({
confirmation: "Success!",
data: users
})
})
.catch((err) => {
res.json({
confirmation: "Fail.",
message: err.message
})
})
})
Mongoose may not have the statement
export default
, then try to care for itimport * as mongoose from 'mongoose'
.– Andre