《Node.js 入門系列》—— 一些簡單的排錯方法(一)

目錄javascript

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

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

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

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 行出錯了;sql

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

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

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

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

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

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 個參數

相關文章
相關標籤/搜索