NodeJS 显示一个错误时,使一个后端的后请求

qyyhg6bp  于 9个月前  发布在  Node.js
关注(0)|答案(1)|浏览(101)

当我做了一个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}`)
});


我想不明白,所以有人能想明白到底发生了什么,并提供建议。

zzwlnbp8

zzwlnbp81#

您需要在CreateUser.js文件中调用express.Router()函数,如下所示:

const router = express.Router();

字符串
此时,你只是将函数赋值给一个名为router的变量:

const router = express.Router;

相关问题