日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《Node.js 入门系列》—— 一些简单的排错方法(一)

發布時間:2023/12/9 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《Node.js 入门系列》—— 一些简单的排错方法(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

TypeError: undefined is not a function
TypeError: Cannot read property 'xxx' of undefined 或者 TypeError: Cannot read property 'xxx' of null
檢查變量是未賦值
檢查函數是否有返回值
檢查變量是否引用了某個對象不存在的屬性
檢查調用函數時是否未該傳遞參數
俗話說“常在河邊走,哪能不濕鞋”,只要動手寫程序,總會時不時的冒出點問題來, 很難一下子就寫出完全正確運行的程序。哪怕只是拿別人的程序來運行,也不能保證其能 適應各種各樣的系統環境,不作任何修改就能使用。因此,學會一些簡單的排錯方法是很 有必要的。

在 Node.js 程序運行過程中,當出現沒有被捕捉到的異常時,程序會打印出相應的出錯 信息,并終止運行。比如以下出錯信息:

f:tmp2013-10-7t.js:3
proceess.nextTick(function () {
^
ReferenceError: proceess is not defined

at Object.<anonymous> (f:\tmp\2013-10-7\t.js:3:1) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3

出錯信息的第 1 行 f:tmp2013-10-7t.js:3 指明了在文件 f:tmp2013-10-7t.js 的第 3 行出錯了;

出錯信息的第 2 行是相應的源程序 proceess.nextTick(function () { ;

出錯信息的第 3 行的 ^ 指明了在該行的具體位置 proceess ;

出錯信息的第 4 行是具體的出錯信息 ReferenceError: proceess is not defined ,后面 還有幾行以 at 開頭的內容是詳細的調用堆棧信息,可以以此來追蹤到整個程序的 執行流程。

當遇到這樣的出錯信息時,我們首先應該看第 4 行的 ReferenceError: proceess is not defined ,前面的 ReferenceError 是錯誤對象, 表示這是一個“非法引用”錯誤,其后便相應的提示信息,大概意思是“ proceess 未定義” (看不懂可以用軟件翻譯一下,比如 有道詞典), 這時候我們再往上看原來的程序是怎么寫的:proceess.nextTick(function () { 。 從這個程序可以看出來,要調用的應該是 process.nextTick() , 此處不小心把 process 寫成了 proceess ,程序自然就報錯“ proceess 未定義”了。

常見的錯誤對象有以下這些:

EvalError : 錯誤發生在 eval() 函數中,一般是要使用 eval() 執行的代碼有語法錯誤
RangeError : 數字的值超過 javascript 可表示的范圍
ReferenceError : 使用了非法的引用,一般是引用了一個未定義的變量或者函數
SyntaxError : 在 eval()函數調用中發生了語法錯誤
TypeError : 變量的類型不是預期所需的
URIError : 在 encodeURI()或者 decodeURI()函數中發生的錯誤
記住這些常見的錯誤對象有助于更快速地理解出錯信息。

TypeError: undefined is not a function

出現這種錯誤的原因是某個變量不是 Function 類型,卻把它當函數來調用了。例如:

帖子: 《node 連接 mysql 出錯》

Node.js 代碼:

var Client = require('mysql').Client;
var client = new Client();
client.host = 'localhost';
client.port = 3306;
client.user = 'root';
client.password = '123456';
client.database='test1';

query(client);

function query (client) {
client.query('select * from user', function (err, res, fields) {

console.log(res); client.end();

});
}
出錯信息:

/home/king/node/mysql.js:2
var client = new Client();

^

TypeError: undefined is not a function

at Object.<anonymous> (/home/king/node/mysql.js:2:14) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3

由出錯信息可以看出,在執行 new Client() 時出錯了, TypeError: undefined is not a function ,也就是說,此時 Client 的值是 undefined 。我們再往上看,可以看到 var Client = require('mysql').Client 那么,應該是 mysql 這個模塊并沒有輸出 Client 這個函數,我們可以執行 console.log(require('mysql')) 來打印 mysql 模塊的輸出,也確定并沒有 Client 這一項,這時候就應該詳細看一下 mysql 模塊幫助文檔以及其正確的使用方法了。

TypeError: Cannot read property 'xxx' of undefined 或者 TypeError: Cannot read property 'xxx' of null

出現這種錯誤的原因是嘗試讀取一個值為 undefined 或 null 的變量的屬性。比如如下代碼:

var a = undefined;
console.log(a.b);
執行該程序將會拋出異常:

TypeError: Cannot read property 'b' of undefined

at repl:1:15 at REPLServer.self.eval (repl.js:110:21) at Interface.<anonymous> (repl.js:239:12) at Interface.EventEmitter.emit (events.js:95:17) at Interface._onLine (readline.js:202:10) at Interface._line (readline.js:531:8) at Interface._ttyWrite (readline.js:760:14) at ReadStream.onkeypress (readline.js:99:10) at ReadStream.EventEmitter.emit (events.js:98:17) at emitKey (readline.js:1095:12)

當出現這種情況時,我們可以通過以下方法來排查:

檢查變量是未賦值

假如只通過 var a 來聲明了變量,但未賦值,此時變量的值為 undefined ,示例:

var a; // 沒有賦值
console.log(a.b);
檢查函數是否有返回值

當函數沒有用 return 來返回一個值時,那么這個函數的返回值就是 undefined , 示例:

function f () {
// 沒有返回值
}
var a = f();
console.log(a.b);
檢查變量是否引用了某個對象不存在的屬性

當引用了某個對象一個不存在的屬性時,其值就是 undefined ,示例:

var obj = {};
var a = obj.c; // 引用了一個不存在的屬性 千鋒PHP-PHP培訓的實力派
console.log(a.b);
檢查調用函數時是否未該傳遞參數

當調用某個函數時沒有按要求傳遞足夠的參數,則在函數體內該參數的值是 undefined , 示例:

function f (a) {
console.log(a.b);
}
f(); // 本來該函數需要 1 個參數

總結

以上是生活随笔為你收集整理的《Node.js 入门系列》—— 一些简单的排错方法(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。