完成注册后,尝试响应POST请求时收到以下错误。 (尽管用户已成功添加到数据库中)
express deprecated res.send(status, body): Use res.status(status).send(body)
instead auth\registrator.js:31:29
D:\startloop\server\node_modules\mysql\lib\protocol\Parser.js:80
throw err; // Rethrow non-MySQL errors
^
RangeError: Invalid status code: [object Object]
at ServerResponse.writeHead (_http_server.js:199:11)
at ServerResponse.writeHead (D:\startloop\server\node_modules\on-headers\index.js:55:19)
at ServerResponse._implicitHeader (_http_server.js:190:8)
at write_ (_http_outgoing.js:632:9)
at ServerResponse.end (_http_outgoing.js:751:5)
at ServerResponse.send (D:\startloop\server\node_modules\express\lib\response.js:221:10)
at ServerResponse.json (D:\startloop\server\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (D:\startloop\server\node_modules\express\lib\response.js:158:21)
at Query.db.query (D:\startloop\server\auth\registrator.js:31:29)
at Query.<anonymous> (D:\startloop\server\node_modules\mysql\lib\Connection.js:502:10)
index.js(以磅为单位)
// Middlewares
const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const bodyParser = require('body-parser')
const validator = require('express-validator')
const registrator = require('./auth/registrator')
const status = require('./appstatus/status')
require('dotenv').config();
// New express app
const app = express()
app.use(morgan('tiny'))
app.use(cors())
app.use(bodyParser.json())
app.use(validator())
// Status GET
app.get('/status', status.getStatus)
// Register POST
app.post('/auth/reg', registrator.register)
// Express.js listen
const port = process.env.PORT || 3362
app.listen(port, () => {
console.log(`On port ${port}`)
})
registrator.js
// Registrator
// Requires request and response
const validator = require('./validator')
const bcrypt = require('bcrypt')
exports.register = function(req, res) {
const valErr = validator.registrationValidator(req)
if (valErr) {
console.log(`Validation Error: ${JSON.stringify(valErr)}`)
res.status(200).send([{well: 'no'} ,valErr])
throw valErr
}
const db = require('../modules/database')
const username = req.body.username
const fname = req.body.fname
const lname = req.body.lname
const email = req.body.email
const password = req.body.password
// Hash Password
bcrypt.hash(password, 10, (err, hash) => {
if (err) throw err
db.query('INSERT INTO users (username, email, password_hash, first_name, last_name) VALUES (?, ?, ?, ?, ?)', [username, email, hash, fname, lname], (err, result) => {
if (err) {
res.send(err)
throw err
}
res.status(200).send({well: 'yes'} ,result)
})
})
return 0
}
这似乎是MySQL的问题。尝试发送valErr可以,但是尝试发送注册响应的方法不起作用。注意:查询已成功完成。
答案 0 :(得分:0)
欢迎,没关系。仔细检查之后,我忘记将响应的数据放在这样的数组中
res.status(200).send([{well: 'yes'}, result])
Damit
答案 1 :(得分:0)
注意错误的第一行
表达已弃用的res.send(status,body):使用res.status(status).send(body)
您的问题在这里
db.query('INSERT INTO users (username, email, password_hash, first_name, last_name) VALUES (?, ?, ?, ?, ?)', [username, email, hash, fname, lname], (err, result) => {
if (err) {
res.send(err)
throw err
}
res.status(200).send({well: 'yes'} ,result)
})
由于错误状态res.send(status, body)
已过时,但您正在执行以下操作:
res.send(err)
它将err
解释为状态码(不是);
将此更改为
res.status(500).send(err);
出现更有意义的错误。
答案 2 :(得分:0)
更改此行代码
res.status(200).send({well: 'yes'} ,result)
收件人
res.sendStatus(res.statusCode);
这将正常工作。.