我无法通过 express.js 中的后端连接到 heroku 上的 psql 数据库。可能是什么原因?请有人回答!
server.js 代码是:
const { handleRegister } = require('./Controllers/Register');
const knex = require('knex');
const db = knex({
client: 'pg',
connection: {
connectionString : process.env.DATABASE_URL,
ssl: true
}
});
app.post('/register', (req,res) => {
handleRegister(req,res,db,bcrypt);
})
register.js 中的代码是:
const saltRounds = 10;
const handleRegister = (req,res,db,bcrypt) => {
const {firstname, lastname, email, password} = req.body;
if(!email || !firstname || !lastname || !password){
return res.status(400).json("Incorrect from submission!");
}
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(password, salt, null , function(err, hash) {
db.transaction(trx => {
trx.insert({
hash: hash,
email: email
})
.into("login")
.returning("email")
.then(mail => {
return trx("users")
.returning('*')
.insert({
email: mail[0],
firstname: firstname,
lastname: lastname,
name: firstname + " " + lastname,
joined: new Date()
})
.then(user => {
res.json(user[0]);
}).catch(err => {res.json("Unable to Register!")});
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err => {
res.json("Error while Registring user!");
})
});
});
}
我收到响应“注册用户时出错!”,但我使用 cmd 成功连接到数据库,这里是 ss:
谁能告诉我如何解决这个问题???