What is Stack trace and how to print in Node.js?

In this tutorial, we will learn the concept of a stack trace by connecting MongoDB with Python and including the functions which furthermore are invoked from Node.js. During execution, errors may arise; which leads to the generation of stack trace that provides the exact location of the error which includes the file name, function name, and line number in the program code that allows the developers to trace the issue.

Table Of Contents

→ Installation
→ Import Libraries
→ What is Stack Trace ?
→ How to print in Nodejs ?
→ Conclusion

Installation

To install MongoDB

pip install pymongo

Import Libraries

import json
import sys
from pymongo import MongoClient
import traceback

What is Stack Trace? 

As a developer, stack traces are the most common errors that you may encounter while performing the program. Basically, a stack is the collection of elements which operates on the principle of Last In, First Out (LIFO) and here, each element represents a function call.

In simple terms, Stack trace is similar to a map that helps us navigate to a destination by showing the correct path. In the Programming, the stack trace indicates the program where the error has occurred. It specifies the exact location of the error through the filename, line number or function call. When you make a mistake in your code, it prints out weird looking messages like,

Exception Type: NameError
Exception Message: name 'collection' is not defined
{"message": "An error occurred during registration"}
Line: 20, Function: register, Code: result = collection.insert_one(query)

We often feel frustrated when we encounter certain types of errors. However, Stack traces can be extremely helpful, as they indicate the exact location of the error by providing a line number, function call or filename. By examining them closely, we can resolve these issues more effectively.

In the above example, the error occurs because the MongoDB collection object is either undefined or inaccessible and the error message, indicates the line number in the program where the issue arises, hence it states that the specified collection is not be found and here, Python is unable to locate the variable named collection. Consequently, Stack trace plays a vital role for identifying and resolving the bugs.

How To Print In Node.js ?

→ Connect Nodejs to MongoDB with Python :
Here, Node.js does not directly accept the Python MongoDB file, so we use the child process to spawn the Python script,
const{spawn} = require('child_process')

In Node.js, we can run Python Scripts using the child_process module. Since Node.js and Python are two separate runtimes, direct interaction between them is not possible. The child_process module allows Node.js to spawn a new process to execute Python Scripts and allows the data exchange between the them. 

const {spawn} = require('child_process');
const python = spawn('python', ['script1.py']);
var dataToSend=" ";
python.stdout.on('data', function (data) {
console.log('Data included in Python script');
dataToSend = data.toString();
});
python.stderr.on('data', (data) => {
console.error(`Error message from Python script: ${data}`);
});
python.on('exit', (code) => {
console.log(`Child process exit the code ${code}`);
console.log(dataToSend);
});

Δ STEPS :

-> HeredataToSend declares a variable and initializes it to an empty string, allowing it to store data generated by a Python script.
-> Thestdout receives the data from the Python script and provided callback functions are executed.
-> Theconsole.log prints the message of the received data from Python script.
-> ThetoString method is to convert the data into a sequence of characters.

var dataToSend=" ";
python.stdout.on('data', function (data) {
console.log('Data included in Python script');
dataToSend = data.toString();
});

If the Python script generates any error messages, these are captured through the stderr stream and stored as the error messages.

python.stderr.on('data', (data) => {
console.error(`Error message from Python script: ${data}`);
});
Once the Python script finishes executing or the process exits, the collected data is displayed, and the program code is successfully executed. 
python.on('exit', (code) => {
console.log(`Child process exit the code ${code}`);
console.log(dataToSend);
});

Therefore using child_process allows the Node.js to interact with and process data from the MongoDB with Python script.

CONCLUSION

In conclusion, a stack trace provides essential context for developers to resolve bugs by identifying the exact location of an error. The first line of the stack trace indicates the last executed function call, which is why it is read from top to bottom. Therefore, the stack trace plays a vital role for developers in debugging their code effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top