Skip to main content
DECAP918-
MULTIPLATFORM MOBILE APP DEVELOPMENT
WITH WEB TECHNOLOGIES
The Kick Start Session
Debugging Error Messages &
Console.log ()
1. Improved understanding of debugging techniques and best practices.
2. Enhanced problem-solving skills when encountering bugs in web projects.
3. Increased familiarity with debugging tools such as browser DevTools and
console.log().
4. Ability to effectively communicate debugging strategies and solutions to team
members or
5. clients.
6. Development of a proactive approach to error handling and code optimization.
7. Confidence in troubleshooting and resolving issues in web
applications
Introduction
Debugging is an indispensable aspect of software development, aiding
in the identification and resolution of issues within your codebase. In
the Ionic framework, a combination of error messages and strategically
placed console.log() statements serve as powerful tools for debugging.
Understanding Error Messages
Error messages are invaluable indicators of what went wrong
within your application. They offer insights into the type of error
encountered and often provide a stack trace, pinpointing the precise
location in your code where the issue occurred.
Common error types include:
Syntax Errors: Occur due to invalid JavaScript syntax
• Runtime Errors: Arise during program execution, such as accessing
undefined variables or functions.
• Logic Errors: Lead to unexpected behavior due to flawed application
logic.
Interpreting error messages involves analyzing the error type, the stack
trace, and any additional context provided. This understanding aids in
swiftly identifying and addressing the root cause of the problem.
Utilizing console.log()
The console.log() function serves as a versatile tool for debugging by
allowing developers to output information to the browser console. Its
usage is straightforward:
Javascript code
• console.log("Debugging message:", variable);
Strategically placing console.log() statements within your code enables
you to track the flow of execution, inspect variable values, and verify
program logic. By logging relevant information at critical points in your
code, you gain insights into the application's internal state and can
diagnose issues effectively.
Practical Application
Consider the following scenario where an Ionic application encounters an error:
Javascript code
function calculateTotal(price, quantity) {
console.log("Price:", price);
console.log("Quantity:", quantity);
const total = price * quantity; // Error occurs here
console.log("Total:", total);
return total;
}
calculateTotal(10, 5);
• In this example, a logic error occurs when calculating the total due to
incorrect variable manipulation. By strategically placing console.log()
statements, developers can inspect the values of price, quantity, and
total to identify the issue promptly.
• Debugging with error messages and console.log() statements is an essential
skill for Ionic developers. By understanding error types, interpreting error
messages, and strategically leveraging
• console.log() statements, developers can efficiently diagnose and resolve
issues within their applications. Incorporating these debugging techniques into
your development workflow ensures the creation of robust and reliable Ionic
applications. Practice and familiarity with these methods empower developers
to tackle challenges effectively and deliver high-quality software solutions.
Understanding Error Messages
Error messages provide valuable insights into what went wrong in your
code. Ionic applications,like any other JavaScript-based project,
generate error messages when something unexpected occurs. These
messages often include stack traces, which pinpoint the location of the
error in your code.
Let's consider an example of an error
message and how to interpret it:
Uncaught TypeError: Cannot read property 'toLowerCase' of undefined at Object.<anonymous>
(app.component.ts:10) at Module._compile (module.js:653) at Module.m._compile (/vendor.js:1) at
Module._extensions..js (module.js:664) at Object.require.extensions.(anonymous function) [as .js]
(/vendor.js:1) at Module.load (module.js:566) at tryModuleLoad (module.js:506) at
Function.Module._load (module.js:498) at Function.Module.runMain (module.js:694) at startup
(bootstrap_node.js:204)
From this error message, we can infer that there's an attempt to call toLowerCase() on an undefined
variable at line 10 of app.component.ts. This insight helps us navigate to the problematic code and
fix the issue.
Browser DevTools Breakpoints
Browser DevTools provide advanced debugging capabilities, including
breakpoints, which allow you to pause the execution of your code at
specific lines. This feature is immensely helpful for inspecting the state
of your application and tracing the flow of execution.
To set a breakpoint:
1. Open DevTools (usually by pressing F12 or right-clicking and
selecting "Inspect").
2. Navigate to the "Sources" tab.
3. Find the relevant JavaScript file containing your code.
4. Click on the line number where you want to set the breakpoint.
• Once a breakpoint is set, the execution of your code will pause at that line
when reached. You can then inspect variables, step through code, and
observe the program's state.
• Debugging is a crucial skill for Ionic developers to master. By understanding
error messages,utilizing console.log(), and leveraging browser DevTools
breakpoints, you can effectively identifyand resolve issues in your Ionic
applications. Practice these techniques regularly, and you'll become
proficient at debugging your code, leading to more robust and reliable apps.
Introduction to Browser DevTools
Breakpoints
• Browser DevTools, available in popular browsers such as Google Chrome,
Mozilla Firefox, and Microsoft Edge, offer a comprehensive suite of
debugging tools, including breakpoints. Breakpoints enable developers to
halt the execution of JavaScript code at predetermined locations,
facilitating in-depth examination of runtime behavior and variable values.
• Browser DevTools breakpoints are indispensable for debugging complex
issues in Ionic applications. They allow developers to pause the execution
of code at specific lines, enabling realtime inspection of variables, function
calls, and program state. This chapter explores the utilization of
breakpoints within Browser DevTools for effective debugging in Ionic
development.
To set breakpoints in an Ionic
application using Browser DevTools
1. Opening DevTools: Launch the application in a supported browser and open
DevTools.This can typically be done by right-clicking on the page and selecting
"Inspect" or pressing
F12.
2. Navigating to Sources Tab: Within DevTools, navigate to the "Sources" or
"Debugger" tab,depending on the browser.
3. Locating Source Files: Locate the relevant source file containing the code to be
debugged.Ionic applications are typically built using TypeScript, so ensure that
you're inspecting the compiled JavaScript files.
4. Setting Breakpoints: Click on the line number in the source file where you want
to set the breakpoint. A red dot will appear, indicating that the breakpoint is active.
Debugging with Breakpoints: A
Practical Example
Consider the following scenario in an Ionic application where an issue arises during the retrieval of
data from an API:
typescriptCopy code
async function fetchData() { try { const response = await fetch('https://api.example.com/data');
const data = await response.json(); processData(data); } catch (error) { console.error('Error fetching
data:', error); } } function processData(data) { // Process data here }
To debug this code using breakpoints:
1. Open DevTools and navigate to the "Sources" tab.
2. Locate the TypeScript file containing the fetchData() function.
3. Set a breakpoint at the beginning of the fetchData() function.
4. Reload the application to trigger the breakpoint.
5. Once the breakpoint is hit, use DevTools to inspect variables, step through code execution,
and diagnose the issue.
Summary
• Debugging error messages involves identifying and resolving issues within code that
trigger error messages during execution.
• Developers often utilize console.log() statements strategically placed throughout their
code to output specific values or messages to the browser console, aiding in
understanding the flow of execution and pinpointing problematic areas.
• Browser DevTools breakpoints offer a more advanced debugging technique by
allowing
• developers to pause code execution at specific points, inspect variables and
expressions, and step through code line by line, facilitating in-depth analysis and
troubleshooting.
• By leveraging a combination of error messages, console logging, and breakpoints
within browser DevTools, developers can efficiently diagnose and resolve issues,
leading to more robust and error-free web applications.
Step 1: Create Ionic Project
• ionic start debugApp blank --type=angular
• cd debugApp
• ionic serve
Step 2: Example 1 – Button Click Debugging
2.1 Update home.page.html
<ion-header>
<ion-toolbar>
<ion-title>Debug Demo</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-button expand="block" (click)="saveData()">
Save Data
</ion-button>
</ion-content>
2.2 Update home.page.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html'
})
export class HomePage {
saveData() {
console.log('Save button clicked');
}
}
2.3 Debug
• Open Chrome DevTools Console
→
• Click button
• ✅ You should see:
• Save button clicked
• 👉 If not event binding problem
→
• Debugging error messages, console.log(), and Browser DevTools
with breakpoints are essential techniques used to identify and fix
issues in applications.
• Error messages provide information about what went wrong in the
code, the type of error (syntax, runtime, or logic), and often the exact
file and line number where the problem occurred. Understanding these
messages helps developers quickly locate the source of the bug.
• console.log() is a simple and effective debugging method used to print
variable values and execution flow to the browser console. By placing
console.log() statements at key points in the code, developers can verify
whether functions are called and whether data is correct.
• Browser DevTools and breakpoints allow developers to pause code
execution at specific lines. This makes it possible to inspect variables,
step through code line by line, and observe how the application behaves
at runtime, enabling deeper and more accurate debugging.