MySQL result from node.js in html, by react.js

362
May 01, 2017, at 07:36 AM

I make my first web application with nodes and reacc. I do sql query in node.js and show html elements in my page. I need help getting the result of sql in my html page as json date. I put some of the code with my experiments, which may be very wrong. Regards.

var React = require('react'); 
var DefaultLayout = require('./layout/master'); 
 
var mysql = require('mysql'); 
 
var connection = mysql.createConnection({ 
    port: '3301', 
    host: 'localhost', 
    user: 'root', 
    password: 'root', 
    database: 'nodedb', 
}); 
connection.connect(); 
 
function getArticless() { 
    connection.query('select * from articles', function(err, result) { 
        if (err)  
            JSON.stringify(err); 
        else 
            JSON.stringify(result); 
    }); 
} 
 
const jsonArticles = getArticless(); 
 
var IndexComponent = React.createClass({ 
    render: function() { 
        return ( 
            <DefaultLayout name={this.props.name}> 
                <div> 
                    {jsonArticles } 
                </div> 
            </DefaultLayout> 
        ) 
    } 
}); 
 
module.exports = IndexComponent;

Answer 1

I am not familiar with the nuances of server side rendering with react, however there is one glaring issue that stands out with your code. I do not know for sure if fixing this will make it all work, but it certainly wont work with this issue.

getArticles is a function that returns nothing and yet you call it as if it is a function that returns something. Now even if you would add a return statement to this function, it will still return undefined because calls to the database happen asynchronously. This means that the function will return before the data was actually retrieved from the db. This function would at the very least require a callback to be passed in, or else you can look in to the mysql-promise library and use that instead.

READ ALSO
Google Actions API webhook response on Heroku nodejs

Google Actions API webhook response on Heroku nodejs

Im trying to use a user's input to be included in the webhook post so that I can use it in my appMy app originally turns house components on or off

478
Passport.js Github Strategy on Node.js: Github redirect_uri mismatch

Passport.js Github Strategy on Node.js: Github redirect_uri mismatch

I want to implement a login with Passportjs and Github Strategy

478
Integrate Disqus in Hexo

Integrate Disqus in Hexo

I have a very basic but very annoyed question for me that I have a Hexo blog and want to integrate Disqus Comments capabilityI'm using the Beantech Hexo theme and it has a disqus_username: kjanshair property in _config

336
Express.js throw exception and return pre-defined status code

Express.js throw exception and return pre-defined status code

In spring i use define my own class extending Exception annotate it with @ResponseStatus to throw it in specific situation to return to user specific status code and error messageI'd like to do something similar in Express

313