Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Express 4.17.1请求主体返回未定义_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Express 4.17.1请求主体返回未定义

Javascript Express 4.17.1请求主体返回未定义,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,代码分为两个文件: 主文件:server.js 路由器文件:user.js 在user.js文件中,req.body返回未定义的,并且不将数据保存到数据库 发送到/user/register的数据返回“无法保存到数据库”。 我尝试了bodyParser.json()和bodyParser.urlencoded({extended:true}),尽管在Express4.17.x中不需要它,而且它们不起作用。 其他常见的解决方案,如app.use(express.json())也没有帮助 serve

代码分为两个文件: 主文件:server.js 路由器文件:user.js 在user.js文件中,req.body返回未定义的,并且不将数据保存到数据库

发送到/user/register的数据返回“无法保存到数据库”。 我尝试了bodyParser.json()和bodyParser.urlencoded({extended:true}),尽管在Express4.17.x中不需要它,而且它们不起作用。 其他常见的解决方案,如app.use(express.json())也没有帮助

server.js

const express = require('express')
const mongoose = require('mongoose')
const User = require("./models/userSchema");

const userRouter = require('./routes/user')
const placeRouter = require('./routes/place');
const bodyParser = require('body-parser');

const app = express()
const PORT = 5000

app.use(express.json())


mongoose.connect('mongodb://localhost:27017/instaDB', {
    useNewUrlParser: true,
    useUnifiedTopology: true
}, () => {
    console.log('Connected to Database');
})

app.use('/user', userRouter)



app.listen(PORT, ()=>{
    console.log(`Listening on PORT: ${PORT}`);
})
user.js

const express = require('express')
const User = require("../models/userSchema");

userRouter = express.Router()

userRouter.post('/register', (req, res) => {
    const {username, password} = req.body
    console.log(req.body.username);
    console.log(password);
    User.findOne({username}, (err, user) => {
        if(err){
            res.status(500).json({
                err: true,
                msgBody: 'Server Error'
            })
        } if(user){
            res.status(400).json({
                err: true,
                msgBody: 'Username already exists'
            })
        }
        else{
            const newUser = new User({
                username,
                password
            })

            newUser.save((err)=>{
                if(err){
                    res.status(500).json({
                        err: true,
                        msgBody: 'Could not save to database'
                    })
                } else {
                    res.status(200).json({
                        err: false,
                        msgBody: 'Registered Successfully'
                    })
                }
            })
        }
    })
})

   
module.exports = userRouter;

我猜初始请求缺少标题
内容类型:application/json

因此,Express json解析器忽略请求主体,因为它不是json类型。
因此解析器不会填充req.body字段

尝试将此标题和此值(
Content-Type:application/json
)添加到您的请求中


您将如何做取决于您如何发送此请求(curl、postman、javascript xhr、react/angular…、lib…

您如何向服务器发出请求?您是否可以添加curl命令或将请求发送到服务器的代码行,这有助于解决此问题?谢谢您的帮助!我错过了我的邮递员ReQuasthi @ KaranMishra的标题,如果这个或任何答案已经解决了你的问题,请考虑点击复选标记。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。