postgreSQL外键不持久

时间:2019-10-02 02:28:50

标签: node.js postgresql

userid没有持久化到数据库。

我有2张桌子。一个表是users,一个表是articles

users表具有3列:

  • userid-主键
  • password
  • username

articles表有6列:

  • articleid-主键
  • datecreated
  • dateupdated
  • body
  • title
  • userid

使用pgAdmin,我在列articeles_userid_foreign_key的列userid上创建了一个名为public.users(userid)的外键。

我的app.js中有;

let userId = req.session.user.userId;

  db.none('INSERT INTO articles(title,body,userid) VALUES($1,$2,$3)', [title, description, userId])
  .then(() => {
    res.send('SUCCESS!!');`enter code here`
  });



app.js

//jshint esversion:8
const express = require('express');
const app = express();
const mustacheExpress = require('mustache-express');
const bodyParser = require('body-parser');
const pgp = require('pg-promise')();
const bcrypt = require('bcrypt');
const session = require('express-session');
const path = require('path');
const PORT = 3000;
const CONNECTION_STRING = {
  host: 'localhost',
  port: 5432,
  database: 'newsdb',
  user: 'postgres',
  password: 'password',
};
const SALT_ROUNDS = 10;

const VIEWS_PATH = path.join(__dirname, '/views');

//CONFIGURING THE VIEW ENGINE
app.engine('mustache', mustacheExpress(VIEWS_PATH + '/partials', '.mustache'));
app.set('views', VIEWS_PATH);
app.set('view engine', 'mustache');

app.use(session({
  secret: 'asdfghjklpoiuytrewq',
  resave: false,
  saveUninitialized: false,
}));

app.use(bodyParser.urlencoded({
  extended: false,
}));

const db = pgp(CONNECTION_STRING);

app.get('/users/add-article', (req, res) => {
  res.render('add-article');
});

app.post('/users/add-article', (req, res) => {
  let datecreated = req.body.datecreated;
  let dateupdated = req.body.dateupdated;
  let title = req.body.title;
  let description = req.body.description;
  let userId = req.session.user.userId;

  db.none('INSERT INTO articles(title,body,userid) VALUES($1,$2,$3)', [title, description, userId])
  .then(() => {
    res.send('SUCCESS!!');
  });
});

app.get('/users/articles', (req, res) => {
  res.render('articles', { username: req.session.user.username });
});

app.post('/login', (req, res) => {
  let username = req.body.username;
  let password = req.body.password;

  db.oneOrNone('SELECT userid, username, password FROM users WHERE username =$1', [username])
  .then((user) => {
    if (user) { //CHECK FOR USERS PASSWORD
      bcrypt.compare(password, user.password, function (error, result) {
        if (result) {
          //PUT USERNAME AND USERID IN SESSION
          if (req.session) {
            req.session.user = { userId: user.userId, username: user.username };
          }

          res.redirect('/users/articles');
        } else {
          res.render('login', { message: 'Invalid Username Or Password!' });
        }
      });
    } else { //IF USER DOES NOT EXIST
      res.render('login', { message: 'Invalid Username Or Password!' });
    }
  });
});

app.get('/login', (req, res) => {
  res.render('login');
});

app.post('/register', (req, res) => {
  let username = req.body.username;
  let password = req.body.password;

  db.oneOrNone('SELECT userid FROM users WHERE username = $1', [username])
    .then((user) => {
      if (user) {
        res.render('register', {
          message: 'Username Already Exists!',
        });
      } else {
        //INSERT USER INTO THE USERS TABLE
        bcrypt.hash(password, SALT_ROUNDS, function (error, hash) {

          if (error == null) {

            db.none('INSERT INTO users(username,password) VALUES($1,$2)', [username, hash])
              .then(() => {
                res.send('SUCCESS');
              });
          }
        });
      }
    });
});

app.get('/register', (req, res) => {
  res.render('register');
});

app.listen(PORT, () => {
  console.log(`Server Has Started on ${PORT}!!!`);
});

因此,我目前分别在各列中分别使用articeleidbody和{{1}获得titledatecreateddateupdated数据}列userid

我了解到nulldatecreated可能配置不正确,但是dateupdated列应具有发布数据的相应userid

我在userid表中获得了userid,所以它确实存在并且在那里没有被输入到users表的userid列中。

0 个答案:

没有答案