我试图在我的POST路由处理器里 使用蒙戈斯制造一个新的文件。 我使用快递。 路由来定义我的路线 。
指数js
const express = require( express )
const app = express()
const userRouter = require( ./routes/user.js )
const mongoose = require( mongoose )
mongoose.connect( mongodb://127.0.0.1:27017/Users )
const userSchema = new mongoose.Schema({
first_name: {
type: String,
required: true
},
last_name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
gender: String
})
const User = new mongoose.model( user , userSchema)
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use( /user , userRouter)
app.listen(3000, () => console.log( Server Started! ))
module.exports = User;
用户js
const express = require( express )
const User = require( ../index )
const router = express.Router()
router.post( / , async (req, res) => {
const newUser = await User.create({
first_name: "John",
last_name: Doe ,
email: [email protected] ,
gender: Male
})
res.send({ status: success , created: newUser })
})
module.exports = router;
不过,我收到以下警告
获得不存在的财产在循环依赖范围内创造模块出口
和差错--
用户. create 不是一个函数
I assume it is saying User
is undefined even though I have exported User
model from 指数js
(apparently).
密码有什么问题?