当我做了一个schem,也做了一个中间件来处理端点,并在数据库上创建模式,它显示我错误,如-
x1c 0d1x的数据
这是我的User.js--
const mongoose = require("mongoose");
const { Schema } = mongoose;
//we are creating schema here
const userSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
location: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
//model is used to make crud operation on userSchema
module.exports = mongoose.model('user', userSchema);
字符串
这是我的博客User.js-
const express = require('express');
const router = express.Router;
const User = require("../models/User"); //use the schema to create user
router.post("/createuser", async (req, res) => { //POST request handler at the endpoint /createuser
try {
await User.create({ //create schema User
name: "Badal",
password: "badal123",
email: "[email protected]",
location: "Jatni"
})
res.json({ seccess: true }); //use to send message to the json like res.send()
} catch (error) {
console.log(error);
res.json({ seccess: false });
}
});
module.exports=router;
型
这是我的索引。js--
const express = require('express')
const mongoDB = require('./db')
const app = express()
const port = 5000
mongoDB();
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.use(express.json());
app.use('/api', require("./Routes/CreateUser"));
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});
型
我想不明白,所以有人能想明白到底发生了什么,并提供建议。
1条答案
按热度按时间zzwlnbp81#
您需要在
CreateUser.js
文件中调用express.Router()
函数,如下所示:字符串
此时,你只是将函数赋值给一个名为
router
的变量:型