Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Explain different way to debug in react and node js

To print all the request values received from the frontend in Node.js, you can access the req.body object. Here's an example of how you can print all the request values in the server-side code:

console.log("yanha aata getMyReviewReport",typeof req.params.item)

  // console.log(req.params.item);
 const item=req.params.item
 const number=Number(item);
 console.log(number);
Enter fullscreen mode Exit fullscreen mode
  const { Question, answerone, answertwo,answerthree,answerfour } = req.body
  console.log(req.body.Question)
Enter fullscreen mode Exit fullscreen mode
  const { role } = req.body
  console.log(req.body)
  console.log(req.body.role)  
Enter fullscreen mode Exit fullscreen mode

in node js

const updateFeedback = async (req, res) => {
  console.log('Request Body:', req.body); // Print the request values to the console

  // Rest of your code...
};
Enter fullscreen mode Exit fullscreen mode

Here are nine different ways to use console.log for debugging in a controller in Node.js:

Basic logging

Use console.log to output values of variables or log general debug information.
Enter fullscreen mode Exit fullscreen mode
console.log('Debug information:', variable);
Enter fullscreen mode Exit fullscreen mode
Logging with labels
Add labels or prefixes to your console.log statements to provide more context.
Enter fullscreen mode Exit fullscreen mode
console.log('[DEBUG] Variable:', variable);
Enter fullscreen mode Exit fullscreen mode
Conditional logging
Use console.log inside conditional statements to selectively log information based on certain conditions.
Enter fullscreen mode Exit fullscreen mode
if (condition) {
  console.log('Debug information:', variable);
}
Enter fullscreen mode Exit fullscreen mode
Logging function outputs
Log the outputs of functions to inspect their return values or debug their behavior.
Enter fullscreen mode Exit fullscreen mode
console.log('Function output:', myFunction());
Enter fullscreen mode Exit fullscreen mode
Logging arrays or objects
Use console.log to inspect the contents of arrays or objects.
Enter fullscreen mode Exit fullscreen mode
console.log('Array:', myArray);
console.log('Object:', myObject);
Enter fullscreen mode Exit fullscreen mode
Logging timestamps
Log timestamps along with your debug information to track the execution time or measure performance.
Enter fullscreen mode Exit fullscreen mode
console.log(new Date(), 'Debug information:', variable);
Enter fullscreen mode Exit fullscreen mode
Logging stack traces
Use console.log to output stack traces for error debugging or to trace the execution flow.
Enter fullscreen mode Exit fullscreen mode
console.log('Stack trace:', new Error().stack);
Enter fullscreen mode Exit fullscreen mode
Logging request details
Log request-related information to troubleshoot issues or analyze request payloads.
Enter fullscreen mode Exit fullscreen mode
console.log('Request method:', req.method);
console.log('Request URL:', req.url);
console.log('Request body:', req.body);
Enter fullscreen mode Exit fullscreen mode

In this example, the /api/data endpoint receives a POST request. By logging specific properties of the req object, you can focus on relevant information for debugging purposes.

req.method: Logs the HTTP method of the request (e.g., GET, POST, PUT, DELETE).
req.url: Logs the URL of the request.
req.headers: Logs the headers sent in the request, including information like content-type, authorization, etc.
req.body: Logs the request body, which contains the payload sent in the request.
Enter fullscreen mode Exit fullscreen mode

Logging loop iterations
Use console.log inside loops to track the iteration progress or inspect loop variables.

for (let i = 0; i < array.length; i++) {
  console.log('Loop index:', i);
  console.log('Array item:', array[i]);
}
Enter fullscreen mode Exit fullscreen mode
app.get('/api/data/:id', (req, res) => {
  console.log('Request parameters:', req.params);
  // Rest of your code...
});
Enter fullscreen mode Exit fullscreen mode

In this example, the /api/data/:id endpoint expects a GET request with an id parameter in the URL. By logging req.params, you can examine the parameters sent in the request.

When a request is made to this endpoint, the req.params object will be logged to the console. It will contain the parameter values passed in the URL, such as id or any other defined parameters.

in react js

 const handleSubmit = async (e) => {
    e.preventDefault()
    console.log("values comes  aaa")
    console.log(values)

}
Enter fullscreen mode Exit fullscreen mode
 const handleChange = (e) => {
    console.log(e.target.name)
    console.log(e.target.value)
    setValues({ ...values, [e.target.name]: e.target.value })
  }
Enter fullscreen mode Exit fullscreen mode
   useEffect(() => {
    if (isClientcustomEditing) {
         console.log('data coming')
         getAllReview().then(
          (data)=>{
            console.log("data asa")
            console.log(data)

          }

        );
      }
  }, [])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)