Thursday 8 July 2021

Where to view console.log output using nodejs/npm start?

very new to react/node/using npm start to start a server. Using npm start to start the server works and displays my react code, but I need to do some debugging. However, my console.logs aren't outputting to the browser console. After that, I thought to check my terminal (which i think is where node console.log outputs to), but since I used npm start, the terminal that I launched the application from is stuck on this screen:


Compiled successfully!

You can now view my-app in the browser.

  Local:            http://localhost:3000/
  On Your Network:  http://[ip_address_here]:3000/

Note that the development build is not optimized.
To create a production build, use npm run build.

And therefore, I cannot read any console output on this screen. I feel like this should be a super basic fix and I'm probably just overlooking something extremely obvious, but could somebody help me out here? Thanks.

EDIT: I was asked to share some code, here it is:

Here is a snippet of my App.js file:


import React from 'react';

//quick class
class App extends React.Component {
    constructor(props){
        super(props);

        this.state = {
            testRows : null
        }

        this.getTest = this.getTest.bind(this);
    }

    //get some rows from my api using a local database with sqlite3
    async getTest(){
        try{
            console.log("hello?");
            const response = await fetch('http://localhost:3000/api/test');
            if(response.ok){
                const JSONresponse = response.json();
                console.log(JSOnresponse);
                this.setState({testRows : JSONresponse});
            }
            else{
                this.setState({testRows: "test error"});
                console.log('There was an error with the fetch');
            }
        }
        catch(error){
            console.log(error);
        }
    }

    //render some text, a button, and the result
    render(){
        let testResults;
        if(this.state.testRows){
            testResults = Object.keys(this.state.testRows).map((data) => {
                    return <h1>{data.name}</h1>
                }
            )
        }
        else{
            testResults = "null";
        }


        return(
            <div>
                <h2>Some testing going on here</h2>
                <button onClick={this.getTest}>
                    Press me for test!
                </button>
                <h3>{testResults}</h3>
            </div>
        );
    }
}

export default App;

Here is a snippet of my api.js file:


apiRouter.get('/test', (req, res, next) => {
    db.get('SELECT * FROM Test', (error, rows) => {
        if(error){
            console.log(error);
        }
        else{
            console.log("got the test");
            console.log(rows);
            res.send(rows);
        }
    })
})

The router is mounted and everything else in other parts of the code, but the console.logs there don't show up anywhere (dev tools console or terminal) either.



from Where to view console.log output using nodejs/npm start?

No comments:

Post a Comment