req.body返回空快递
req.body returning null Express
学校项目有问题。我正在尝试发出 POST 请求,通过 Insominia 将新用户插入 table。我不断收到用户名的空值错误。我已将我的代码发送给其中一位助教,但他们没有收到此错误。该代码对他们来说工作正常。所以我们无法找出我这边的问题。我想知道这是否与 Insomina 有关。如有任何帮助,我们将不胜感激。
错误信息:
{
"error": {
"message": "null value in column \"username\" of relation \"users\" violates not-null constraint",
"status": 500
}
}
我通过 Insomina 的请求:
{
"username": "newUser",
"password": "password"
}
index.js
app.use(express.json());
用户 class 的 ORM,createNewUser 函数用于 POST 请求。
user.js
/* Create a new user function for post request. Inserting user name and password.
Returning the id, username, and password. */
static async createNewUser(username, password) {
const results = await db.query(
`INSERT INTO users (username, password)
VALUES (, )
RETURNING id, username, password`,
[username, password]);
console.log(results.rows);
let { id } = results.row[0];
return new User(id, username, password);
}
POST 路由给我带来麻烦,req.body 返回未定义。
userRoutes.js
/* Create a new user by requesting the body of username and password
and returning the json.*/
router.post('/', async function (req, res, next) {
try {
console.log(req.headers);
let id = await User.createNewUser(req.body.username, req.body.password);
console.log(id);
return res.json(id);
} catch (e) {
return next(e);
}
});
Header 登录终端。我想知道我的问题是否出在这里,因为我没有得到 Content-Type: application/json.
{
host: 'localhost:3001',
'user-agent': 'insomnia/2021.4.1',
authorization: 'Basic Og==',
accept: '*/*',
'content-length': '51'
}
headers 在 Insomina
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 121
ETag: W/"79-wzArjA/Y97Lx74leuLv1MyJkXDQ"
Date: Mon, 06 Sep 2021 16:49:00 GMT
Connection: keep-alive
Keep-Alive: timeout=5
学校项目有问题。我正在尝试发出 POST 请求,通过 Insominia 将新用户插入 table。我不断收到用户名的空值错误。我已将我的代码发送给其中一位助教,但他们没有收到此错误。该代码对他们来说工作正常。所以我们无法找出我这边的问题。我想知道这是否与 Insomina 有关。如有任何帮助,我们将不胜感激。
错误信息:
{
"error": {
"message": "null value in column \"username\" of relation \"users\" violates not-null constraint",
"status": 500
}
}
我通过 Insomina 的请求:
{
"username": "newUser",
"password": "password"
}
index.js
app.use(express.json());
用户 class 的 ORM,createNewUser 函数用于 POST 请求。
user.js
/* Create a new user function for post request. Inserting user name and password.
Returning the id, username, and password. */
static async createNewUser(username, password) {
const results = await db.query(
`INSERT INTO users (username, password)
VALUES (, )
RETURNING id, username, password`,
[username, password]);
console.log(results.rows);
let { id } = results.row[0];
return new User(id, username, password);
}
POST 路由给我带来麻烦,req.body 返回未定义。
userRoutes.js
/* Create a new user by requesting the body of username and password
and returning the json.*/
router.post('/', async function (req, res, next) {
try {
console.log(req.headers);
let id = await User.createNewUser(req.body.username, req.body.password);
console.log(id);
return res.json(id);
} catch (e) {
return next(e);
}
});
Header 登录终端。我想知道我的问题是否出在这里,因为我没有得到 Content-Type: application/json.
{
host: 'localhost:3001',
'user-agent': 'insomnia/2021.4.1',
authorization: 'Basic Og==',
accept: '*/*',
'content-length': '51'
}
headers 在 Insomina
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 121
ETag: W/"79-wzArjA/Y97Lx74leuLv1MyJkXDQ"
Date: Mon, 06 Sep 2021 16:49:00 GMT
Connection: keep-alive
Keep-Alive: timeout=5