DEV Community

Discussion on: Question about connecting React Router to a database

Collapse
 
murindwaz profile image
Pascal Maniraho • Edited

I am not sure but here are two things:

  • return dataset expected on your downstream points(react router or other system)
  • Don't return console.log()
  • make error more explicit( the error object is forwarded to downstream points(react router) to get more ideas on what is not working in your case.
//....
connection.query('SELECT * FROM chirps', (err, results) => {
    var rs = Object.assign({}, results || {});    
    connection.end();
    if(err) {
            console.log(err);
            return res.status(500).json({error: err || "Something went wrong"});
        }
        return res.status(200).json(rs);
    });

I hope this helps!

Collapse
 
christycakes profile image
Christy

That did help, thanks! It gave me the error 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR' which led me to this post: github.com/mysqljs/mysql/issues/900. I think it's a little beyond me, but basically using a single connection leads to this common error; so it's better to use a pool.

Collapse
 
murindwaz profile image
Pascal Maniraho

I am glad that it helped ~ Happy coding ;-)