
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - console.error() 方法
Node.js 的 console.error() 方法 將錯誤訊息顯示在 控制檯 上。它在 stderr 上列印錯誤行,並新增換行符。此方法用於建立 Error 類的例項,Error 類是自定義錯誤的基類。
錯誤物件包含諸如名稱、訊息和堆疊跟蹤之類的屬性,可以使用點(.)或括號()表示法根據使用的環境進行訪問。
假設我們想在控制檯上列印一些錯誤訊息。那麼這個 console.error() 就發揮作用了。為了更好地理解,讓我們深入瞭解 Node.js 的 console.error() 函式的語法和用法。
語法
以下是 Node.js 的 console.error() 方法 的語法:
console.error([data][, …args])
引數
此方法接受多個引數。下面描述了這些引數。
data - 此引數中的值用作主要訊息。
args - 此引數中的值用作替換值(所有引數都傳遞給 util.format()),可以在主要訊息中使用。
返回值
此方法將錯誤訊息列印到 stderr,並新增換行符。
示例
Node.js 的 console.error() 方法接受多個引數。
在下面的示例中,我們正在檢查兩個變數;如果兩者相等,我們列印正常訊息;否則,我們列印錯誤訊息。
var x = 5; var y = 7; if (x === y){ console.log("Both variables are equal") } else{ console.error("Both variables are not equal") }
輸出
正如我們在輸出中看到的,我們使用 %s(用於 字串值)訪問了替換值,並在 控制檯 上列印了錯誤訊息。
Both variables are not equal
示例
在下面的示例中,我們正在檢查兩個變數;如果兩者相等,我們列印正常訊息;否則,我們列印錯誤訊息。此外,我們還使用其他引數作為主要訊息的附加替換值。
var x = 5; var y = 7; if (x === y){ console.log("The variables %d and %d are equal", x, y) } else{ console.log("The variables %d and %d are not equal", x, y) }
輸出
正如我們在輸出中看到的,我們使用 %d(用於整數)訪問了替換值,並根據條件的輸出在 控制檯 上列印了錯誤訊息。
The variables 5 and 7 are not equal
示例
在下面的示例中,我們使用 args 引數作為主要訊息的附加替換值。
console.error("%s: Simply Easy %s at your %s", "Tutorialspoint", "Learning", "fingertips"); console.error("Sachin score hundred %d", 100); console.error("Hi %s Namaste %d %d %d", "hello", 1, 2, 3);
輸出
正如我們在輸出中看到的,我們使用 %s(用於字串值)和 %d(用於整數)訪問了多個替換值,並在控制檯上列印了錯誤訊息。
Tutorialspoint: Simply Easy Learning at your fingertips Sachin score hundred 100 Hi hello Namaste 1 2 3