Node.js Insert comments into MySQL second table with foreign key –

I have two tables in my database:

Users: enter image description here


comments: enter image description here

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:"/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');"/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;       , 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.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.