我的POST请求创建用户时遇到问题。 当我用邮递员对其进行测试时,它在加载时保持阻塞状态。如果我取消了它,我将在终端中保存它:
xml
有相关代码:
POST /users/create_account - - ms - -
Oc,在我的邮递员身体中,我知道:
const client = new pg.Client({
user: process.env.DB_DBUSER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASS,
port: process.env.DB_PORT,
})
//Third-party middelware
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({
extended: true
}));
router.post('/create_account', function (req, res, next) {
const results = [];
client.connect();
// Grab data from http request
const data = {
username: req.body.username, name: req.body.user,
firstname: req.body.firstname, email: req.body.email,
location: req.body.location
};
// Get a Postgres client from the connection pool
client.on('error', (err) => {
// Handle connection errors
if (err) {
console.log(err);
return res.status(500).json({ success: false, data: err });
}
const queryInsert = 'INSERT INTO Users(username, name,'
+ 'firstname, email, location) values($1, $2, $3, $4, $5)';
const values = [data.username, data.name,
data.firstname, data.email, data.location];
// SQL Query > Insert Data into Users
client.query(queryInsert, values, (err, res) => {
if (err) {
console.log(err.stack)
} else {
queryInsert.on('end', () => {
Promise.all([queryInsert])
.then(() => client.end()).catch(err => console.log(err));
return res.json(results);
});
};
});
});
我的依赖项:
{
"username" : "Username",
"name" : "Bob",
"firstname" : "Paul",
"email" : "spfqqds@gmail.com",
"location" : "Here"
}
如果有人可以帮助我,他/她将是我的救星!提前致谢。