Define and export the jwtInterceptor function as below: (save in jwtInterceptor.ts)
import { HttpRequest, HttpHandlerFn } from '@angular/common/http';
This blog is dedicated to share my experience during my development as a purpose of notes and explorer various web / enterprise technologies like JAVA , JEE , Spring ,hybris, Portal , Jquery , RAI , JMS, Weblogic , SSL , Security, CS, MAC< Linux, Windows, Search, IOT, Arduino, Machine Learning, Tips, Angular, Node JS, React, Mac, Windows, Stack, Exception, Error etc. with examples.
Define and export the jwtInterceptor function as below: (save in jwtInterceptor.ts)
import { HttpRequest, HttpHandlerFn } from '@angular/common/http';
One of the most common errors encountered by JavaScript developers, especially when working with arrays and React, is the infamous:
TypeError: Cannot read property 'map' of undefinedThis error indicates that the code is trying to call the map method on a value that is undefined. Here, we’ll break down the causes of this error and provide actionable solutions to prevent it.
The map method is used to iterate over an array and transform its elements. If the value you’re trying to call map on isn’t an array (e.g., undefined, null, or another non-array type), JavaScript will throw this error.
Undefined or null data:
const items = undefined;
items.map(item => console.log(item)); // TypeErrorAsynchronous data loading: When fetching data from an API, the array might not be available immediately.
const [data, setData] = useState();
useEffect(() => {
fetch('/api/data')
.then(response => response.json())
.then(fetchedData => setData(fetchedData));
}, []);
data.map(item => console.log(item)); // TypeErrorIncorrect initial value:
You might set the state to undefined or forget to initialize it as an empty array.
Always initialize variables that will hold arrays with an empty array. This ensures the map method can be safely called.
const items = [];
items.map(item => console.log(item)); // Safe, no errorOptional chaining (?.) prevents accessing properties or methods on undefined or null.
items?.map(item => console.log(item)); // No error, but does nothing if items is undefinedUsing default parameters or the || operator ensures a fallback array if the value is undefined.
function processItems(items = []) {
items.map(item => console.log(item));
}(items || []).map(item => console.log(item));mapExplicitly validate the type of the variable to ensure it is an array.
if (Array.isArray(items)) {
items.map(item => console.log(item));
} else {
console.log('items is not an array');
}When working with APIs or asynchronous operations, make sure to handle the loading state properly.
const [data, setData] = useState([]); // Initialize with an empty array
useEffect(() => {
fetch('/api/data')
.then(response => response.json())
.then(fetchedData => setData(fetchedData))
.catch(error => console.error('Error fetching data:', error));
}, []);
return (
<div>
{data.map(item => (
<p key={item.id}>{item.name}</p>
))}
</div>
);If you encounter this error, follow these steps to debug it:
Log the Variable:
Use console.log to check the value of the variable before calling map.
console.log(items);Check Data Flow: Trace the origin of the variable to ensure it’s being set correctly.
Handle Edge Cases:
Consider scenarios where the variable might be undefined or null, especially when working with dynamic or external data.
Updating Node.js Applications Without Manual Restart Using Nodemon
When developing Node.js applications, manually restarting the server every time you make changes to your code can be both time-consuming and tedious. Thankfully, tools like Nodemon simplify the development workflow by automatically restarting your Node.js application whenever a file changes. This article explores how to set up and use Nodemon to enhance your productivity.
Nodemon is a utility that monitors changes in your Node.js application files and automatically restarts the server when a change is detected. It’s particularly useful for developers who need rapid feedback while coding.
Automatic server restarts on file changes.
Supports custom configurations for monitoring specific files or directories.
Compatible with most Node.js frameworks and libraries.
Lightweight and easy to set up.
To get started with Nodemon, you’ll need to have Node.js and npm (Node Package Manager) installed on your system.
Open your terminal.
Install Nodemon globally by running:
npm install -g nodemonAlternatively, you can add it as a development dependency to your project:
npm install --save-dev nodemonUsing Nodemon to run your application is simple. Instead of starting your app with node, use nodemon:
nodemon app.jsand if nodemon is not installed globally then execute following:
./node_modules/.bin/nodemon app.js
Here, app.js is the entry point of your application. Nodemon will now monitor your project files and automatically restart the server when any changes are detected.
Nodemon allows you to create a configuration file for more advanced setups. By default, it looks for a nodemon.json file in the root of your project. Here’s an example configuration:
{
"watch": ["src"],
"ext": "js,json,html",
"ignore": ["node_modules"],
"exec": "node src/index.js"
}watch: Specifies the directories or files to monitor.
ext: Defines the file extensions to watch.
ignore: Excludes specific files or directories from monitoring.
exec: Specifies the command to run your application.
Nodemon also supports various command-line options for customization:
Watch Specific Files/Directories:
nodemon --watch srcIgnore Files/Directories:
nodemon --ignore logsSpecify Extensions to Watch:
nodemon --ext js,htmlHere are a few tips to make the most of Nodemon:
Combine with Environment Variables: Use environment variables to manage different configurations:
nodemon -e js --exec "NODE_ENV=development node app.js"Integrate with npm Scripts:
Define a script in your package.json for easy use:
"scripts": {
"start:dev": "nodemon app.js"
}Then run:
npm run start:devUse Debugging Tools: Nodemon works seamlessly with Node.js debugging tools. Start your app in debug mode:
nodemon --inspect app.jsNodemon is a powerful tool that eliminates the hassle of manually restarting your Node.js server during development. With its flexibility and ease of use, it’s a must-have for any Node.js developer looking to streamline their workflow.
Set up Nodemon today and enjoy a smoother, more efficient development experience!
Cheers,
Kapil
If we get following warning :
Warning: bundle initial exceeded maximum budget.
then to solev this we need to increase budget in angular.json
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
Cheers,
http://download.eclipse.org/buildship/updates/e45/releases/1.0http://download.eclipse.org/buildship/updates/e44/releases/1.0http://download.eclipse.org/buildship/updates/e43/releases/1.0http://download.eclipse.org/buildship/updates/e42/releases/1.0http://download.eclipse.org/buildship/updates/e37/releases/1.http://download.eclipse.org/buildship/updates/e36/releases/1.0