req.body 返回一个空对象 // 输出
Posted
技术标签:
【中文标题】req.body 返回一个空对象 // 输出【英文标题】:req.body returns an empty object // output 【发布时间】:2022-01-23 00:43:00 【问题描述】:从客户端发送请求返回一个空对象,我已经检查了 *** 上所有可能的解决方案,似乎没有一个对我有用。 我真的需要帮助,因为我已经被困了两个多星期了。
在下面查看我的 html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register User</title>
<link rel="stylesheet" href="app.css"/>
</head>
<body>
<form action="register.js" id="mForm" method="POST">
<div>
<label for="nama"> Firstname</label>
<input type="text" id="nama" name="nama" required>
</div>
<br>
<div>
<label for="namo"> Lastname</label>
<input type="string" id="namo" name="namo" required>
</div>
<br>
<div>
<label for="password"> password</label>
<input type="password" id="password" name="password" required>
</div>
<br>
<div>
<label for="email"> email</label>
<input type="email" id="email" name="email" required>
</div>
<br>
<div>
<button type="submit" >signup</button>
</div>
</form>
<br>
<div id="det">
</div>
<script src="register.js"></script>
</body>
</html>
<a href="login.html"> login user</a>
虽然使用 xmlHttpRequest 获取代码如下所示:
var form = document.getElementById('mForm');
form.addEventListener('submit', sendData);
function sendData(event)
event.preventDefault();
var namo = document.getElementById('namo').value;
var nama = document.getElementById('nama').value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
var params = JSON.stringify(
nama,
namo,
password,
email
);
var vali = params;
const XHR = new XMLHttpRequest();
XHR.open("POST", "http://localhost:5000/admin/pass", true);
XHR.setRequestHeader("Content-Type", "applicaion/json");
XHR.onload = function ()
const value = this.responseText;
const deta = document.getElementById('det');
deta.innerHTML = `
<br>
<div>$value</div>
<div>Firstname:$nama</div>
<div>Lastname:$namo</div>
<div>Email: $email</div>
<br>
`
console.log(value);
;
XHR.send(vali);
下面是架构(admin.js),如果我在架构中包含 required:true ,它会返回所需路径的验证错误。
const mongoose = require ('mongoose');
const adminSchema = new mongoose.Schema(
nama:
type: String
,
namo:
type: String
,
email:
type: String
,
password:
type: String,
required: true
,
);
adminSchema.index(email: 1, unique: true);
module.exports = mongoose.model('admin', adminSchema);
下面是返回空的路由器:
let express = require('express');
let router = express.Router();
const Admin = require('../model/admin');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const app = express();
app.use(express.json());
router.post('/pass', express.json(), async function(req, res)
res.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
res.setHeader("Access-Control-Expose-Headers", "Content-Type, application/json;charset=utf-8");
const nama, namo, email, password = req.body;
console.log(req.body);
return res.status(201).send(response);
);
当控制器在下方时:
if (process.env.NODE_ENV !== 'production')
require('dotenv').config();
const express = require('express');
const app = express();
const cors = require('cors');
const methodOverride = require('method-override');
const registerRouter = require('./routes/register');
const parcelRouter = require('./routes/parcel');
const adminRouter = require ('./routes/admin');
const path = require('path');
const router = express.Router();
const mongoose = require('mongoose');
mongoose.connect(process.env.DATABASE_URL,
useNewUrlParser: true, useUnifiedTopology: true);
const db = mongoose.connection;
db.on('error', error => console.error(error));
db.once('open', () => console.log('connected to Mongoose'));
app.use(express.json());
app.use(express.urlencoded(extended: false));
app.use(express.text(type: 'application/json'));
app.use('/', express.static(path.join(__dirname, 'static')));
app.use(cors());
app.use(methodOverride('_method'));
app.use('/admin', adminRouter);
app.use('/parcels/', router);
app.use('/register', registerRouter);
app.use('/user/', router);
app.use('/parcel', parcelRouter);
app.listen(process.env.PORT || 5000);
【问题讨论】:
【参考方案1】:在上面的代码中,我没有找到在 return res.status(201).send(response);
任何地方声明的 response
变量。您似乎忘记定义这可能是它响应的原因
【讨论】:
响应变量已声明但省略,因为我的焦点区域是返回空对象的 console.log(req.body)以上是关于req.body 返回一个空对象 // 输出的主要内容,如果未能解决你的问题,请参考以下文章