I have two tables in my database:
and
Users authenticated with Passport.js
can enter rooms. When these logged in users sent a message, I need the message to go to the comments table, which has a foreign key from the user's table.
I'm not sure if I'm using the right solution. But here's what I tried to use, I know it's wrong, but how would I alter to get the comments of logged in users to my comments table:
router.post("/superhero-movies", function(req, res) {
const { comments } = req.body;
const sqlDatabase = require('../db.js');
sqlDatabase.query('INSERT INTO comments (comments) VALUES (?)', [comments], function(error, results, fields) {
if (error) throw error;
console.log(results);
return res.render('superhero-movies')
})
})
router.get("/superhero-movies", authenticationMiddleware(), function(req, res) {
res.render('superhero-movies');
router.post("/login",
passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
// failureFlash: true,
}));
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true,
},
function(req, email, password, done) {
console.log(email, password, done);
const sqlDatabase = require('../db.js');
sqlDatabase.query('SELECT * FROM users WHERE email = ?', [email], (err, results, fields, res) => {
if (err)
return done(err);
console.log(err, fields, results);
if (results.length === 0) {
done(null, false, { message: 'Check email and password again.' });
} else {
const hash = results[0].
password.toString();
const user_id = results[0].id;
bcrypt.compare(password, hash, function(err, response) {
console.log(user_id);
if (response === true) {
return done(null, user_id);
} else {
console.log(user_id);
console.log(err);
return done(null, false, { message: 'This is a test notification.' });
}
})
}
})
}));
passport.serializeUser(function(user_id, done) {
console.log(user_id);
done(null, user_id);
console.log(done);
console.log('serialized', user_id);
});
passport.deserializeUser(function(user_id, done) {
done(null, user_id);
console.log(done);
console.log('deserialized', user_id);
});
sqlMessage: 'Cannot add or update a child row: a foreign key constraint fails (`app_users`.`comments`, CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`comments_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)',
Not sure if I'm supposed to use a split query with select and insert, or if I'm going about this whole thing the wrong way.
Excuse the mishandling after the insert statement. Will fix it.
I know I posted this question a while back, but I just want to post the answer in case someone might need it in the future.
router.post("/superhero", function(req, res) {
const user = req.user;
const comments = req.body.comments;
sqlDatabase.query("INSERT INTO comments (user_id, comments) VALUES (?, ?)", [user, comments],
function(error, results, fields) {
if (error) throw error;
console.log(results);
console.log(comments);
console.log(error)
});
})
That req.user is from passport authentication.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.