《Node.js 入门系列》—— 一些简单的排错方法(一)
目錄
TypeError: undefined is not a function
TypeError: Cannot read property 'xxx' of undefined 或者 TypeError: Cannot read property 'xxx' of null
檢查變量是未賦值
檢查函數(shù)是否有返回值
檢查變量是否引用了某個(gè)對(duì)象不存在的屬性
檢查調(diào)用函數(shù)時(shí)是否未該傳遞參數(shù)
俗話說(shuō)“常在河邊走,哪能不濕鞋”,只要?jiǎng)邮謱懗绦?#xff0c;總會(huì)時(shí)不時(shí)的冒出點(diǎn)問(wèn)題來(lái), 很難一下子就寫出完全正確運(yùn)行的程序。哪怕只是拿別人的程序來(lái)運(yùn)行,也不能保證其能 適應(yīng)各種各樣的系統(tǒng)環(huán)境,不作任何修改就能使用。因此,學(xué)會(huì)一些簡(jiǎn)單的排錯(cuò)方法是很 有必要的。
在 Node.js 程序運(yùn)行過(guò)程中,當(dāng)出現(xiàn)沒(méi)有被捕捉到的異常時(shí),程序會(huì)打印出相應(yīng)的出錯(cuò) 信息,并終止運(yùn)行。比如以下出錯(cuò)信息:
f:tmp2013-10-7t.js:3
proceess.nextTick(function () {
^
ReferenceError: proceess is not defined
出錯(cuò)信息的第 1 行 f:tmp2013-10-7t.js:3 指明了在文件 f:tmp2013-10-7t.js 的第 3 行出錯(cuò)了;
出錯(cuò)信息的第 2 行是相應(yīng)的源程序 proceess.nextTick(function () { ;
出錯(cuò)信息的第 3 行的 ^ 指明了在該行的具體位置 proceess ;
出錯(cuò)信息的第 4 行是具體的出錯(cuò)信息 ReferenceError: proceess is not defined ,后面 還有幾行以 at 開頭的內(nèi)容是詳細(xì)的調(diào)用堆棧信息,可以以此來(lái)追蹤到整個(gè)程序的 執(zhí)行流程。
當(dāng)遇到這樣的出錯(cuò)信息時(shí),我們首先應(yīng)該看第 4 行的 ReferenceError: proceess is not defined ,前面的 ReferenceError 是錯(cuò)誤對(duì)象, 表示這是一個(gè)“非法引用”錯(cuò)誤,其后便相應(yīng)的提示信息,大概意思是“ proceess 未定義” (看不懂可以用軟件翻譯一下,比如 有道詞典), 這時(shí)候我們?cè)偻峡丛瓉?lái)的程序是怎么寫的:proceess.nextTick(function () { 。 從這個(gè)程序可以看出來(lái),要調(diào)用的應(yīng)該是 process.nextTick() , 此處不小心把 process 寫成了 proceess ,程序自然就報(bào)錯(cuò)“ proceess 未定義”了。
常見的錯(cuò)誤對(duì)象有以下這些:
EvalError : 錯(cuò)誤發(fā)生在 eval() 函數(shù)中,一般是要使用 eval() 執(zhí)行的代碼有語(yǔ)法錯(cuò)誤
RangeError : 數(shù)字的值超過(guò) javascript 可表示的范圍
ReferenceError : 使用了非法的引用,一般是引用了一個(gè)未定義的變量或者函數(shù)
SyntaxError : 在 eval()函數(shù)調(diào)用中發(fā)生了語(yǔ)法錯(cuò)誤
TypeError : 變量的類型不是預(yù)期所需的
URIError : 在 encodeURI()或者 decodeURI()函數(shù)中發(fā)生的錯(cuò)誤
記住這些常見的錯(cuò)誤對(duì)象有助于更快速地理解出錯(cuò)信息。
TypeError: undefined is not a function
出現(xiàn)這種錯(cuò)誤的原因是某個(gè)變量不是 Function 類型,卻把它當(dāng)函數(shù)來(lái)調(diào)用了。例如:
帖子: 《node 連接 mysql 出錯(cuò)》
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) {
});
}
出錯(cuò)信息:
/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由出錯(cuò)信息可以看出,在執(zhí)行 new Client() 時(shí)出錯(cuò)了, TypeError: undefined is not a function ,也就是說(shuō),此時(shí) Client 的值是 undefined 。我們?cè)偻峡?#xff0c;可以看到 var Client = require('mysql').Client 那么,應(yīng)該是 mysql 這個(gè)模塊并沒(méi)有輸出 Client 這個(gè)函數(shù),我們可以執(zhí)行 console.log(require('mysql')) 來(lái)打印 mysql 模塊的輸出,也確定并沒(méi)有 Client 這一項(xiàng),這時(shí)候就應(yīng)該詳細(xì)看一下 mysql 模塊幫助文檔以及其正確的使用方法了。
TypeError: Cannot read property 'xxx' of undefined 或者 TypeError: Cannot read property 'xxx' of null
出現(xiàn)這種錯(cuò)誤的原因是嘗試讀取一個(gè)值為 undefined 或 null 的變量的屬性。比如如下代碼:
var a = undefined;
console.log(a.b);
執(zhí)行該程序?qū)?huì)拋出異常:
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)當(dāng)出現(xiàn)這種情況時(shí),我們可以通過(guò)以下方法來(lái)排查:
檢查變量是未賦值
假如只通過(guò) var a 來(lái)聲明了變量,但未賦值,此時(shí)變量的值為 undefined ,示例:
var a; // 沒(méi)有賦值
console.log(a.b);
檢查函數(shù)是否有返回值
當(dāng)函數(shù)沒(méi)有用 return 來(lái)返回一個(gè)值時(shí),那么這個(gè)函數(shù)的返回值就是 undefined , 示例:
function f () {
// 沒(méi)有返回值
}
var a = f();
console.log(a.b);
檢查變量是否引用了某個(gè)對(duì)象不存在的屬性
當(dāng)引用了某個(gè)對(duì)象一個(gè)不存在的屬性時(shí),其值就是 undefined ,示例:
var obj = {};
var a = obj.c; // 引用了一個(gè)不存在的屬性 千鋒PHP-PHP培訓(xùn)的實(shí)力派
console.log(a.b);
檢查調(diào)用函數(shù)時(shí)是否未該傳遞參數(shù)
當(dāng)調(diào)用某個(gè)函數(shù)時(shí)沒(méi)有按要求傳遞足夠的參數(shù),則在函數(shù)體內(nèi)該參數(shù)的值是 undefined , 示例:
function f (a) {
console.log(a.b);
}
f(); // 本來(lái)該函數(shù)需要 1 個(gè)參數(shù)
總結(jié)
以上是生活随笔為你收集整理的《Node.js 入门系列》—— 一些简单的排错方法(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Bash中执行存储过程或普通的SQL命令
- 下一篇: grep的时候Binary file (