有一次,该港口处于活动状态并正常工作,但邮递员没有回应。
现在它甚至不识别表达,即使它被包括在内。有人可以帮我一把,我一直试图解决这个问题好几天了...
我尝试使用knex更改为连接到postgres数据库,在Pg admin中创建了数据库,但它不起作用。我想要有几条路由登录,注册和删除用户配置文件,但似乎Express因某些原因无法工作。我试图将app.post(注册)中的.catch更改为.catch(err => res.json(err)),同时我为Chrome安装了corse扩展程序,但它也没有用。我修改了代码中的一些单词(来自我的母语,因此更容易复习,如果有一个我错过的地方,我道歉...我必须粘贴整个代码,因为错误可能在我省略的部分中。此时,在输出中,Express未被识别为已安装。
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const app = express();
app.use(bodyParser.json());
app.use(cors());
let db = knex({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : '12345',
database : 'users'
}
});
/*app.get('/', (req, res) => {
db.select('*').from('users').then(data => {res.json(data)});
})*/
//USER LOGIN
app.post('/signin', (req, res) => {
db.select('email', 'hash').from('login')
.where('email', '=', req.body.email)
.then(data => {
const isValid = bcrypt.compareSync(req.body.password, data[0].hash);
if (isValid) {
return db.select('*').from('login')
.where('email', '=', req.body.email)
.then(user => {
res.json(user[0])
})
.catch(err => res.status(400).json('Cannot find user'))
} else {
res.status(400).json('Incorrect log in data')
}
})
.catch(err => res.status(400).json('Incorrect data'))
});
// REGISTER USER
app.post('/register', (req, res) => {
const {email, name, password} = req.body;
const hash = bcrypt.hashSync(password);
db.transaction(trx => {
trx.insert({
hash: hash,
email: email
})
.into('login')
.returning('email')
.then(loginEmail => {
return trx('users')
.returning('*')
.insert({
email: loginEmail[0],
name: name,
resgistered: new Date()
})
.then(user => {
res.json(user[0]);
})
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err =>res.json(err))
});
// user profile
app.get('/profile/:id', (req, res) => {
const {id} = req.params;
db.select('*').from('users').where( {id: id})
.then(user => {
if (user.length) {
res.json(user[0])
} else {
res.status(400).json('User not found')
}
});
});
app.get('/allusers', (req, res) => {
//const {id} = req.params;
//db.select('*').from('korisnici').then(data => {console.log(data)});
db.select('*').from('users').then(data => {res.json(data)});
});
//delete user
app.delete('/users/:name', (req, res) =>{
const email = req.params.email;
db.select()
.from('users').where({email: email}).del()
.then((users) =>{
db.select()
.from('login').where({email: email}).del()
.then(() => {
res.json(`user ${email} deleted`);
});
}).catch((error) => {
console.log(error);
});
});
//APPLICATION PORT
app.listen(3000, () =>{
console.log(('Port 3000 active'));
//res.send('Database active at port 3000')
});
确保你安装了pg
模块:
npm install pg --save
你确定你的db叫做users
吗?如果是这样,那么在你的情况下,你需要在login
db中使用users
表。然后还将pg admin中的一些数据插入到users.login
中。
您也可以尝试使用SQL查询直接从pg admin查询数据,以确保数据实际存在:
SELECT * FROM users.login;
然后创建以下路由:
app.get('/', (req, res) => {
db.select('*').from('login').then(data => {
console.log(data);
res.json(data);
});
});
然后尝试发送邮递员GET http://localhost:3000
的请求。
注意:我建议你在db中使用正确的表命名,不确定是否要调用表login
。并尝试使用async \ await而不是使用.then
进行嵌套
另一个解决方案是在这个简单的tutorial之后从头开始创建一个项目