[英]How to fix “Cannot read property 'trim' of undefined” error in node
我正在用TypeScript表達的Firebase雲函數中實現一個簡單的WebAPI。 我的代碼如下。
import * as functions from 'firebase-functions';
import * as express from 'express';
const app = express()
app.post('/', (req, res) => {
var resText: string = "NotEmpty"
const text: string = req.body.text
if (isEmpty(text)) {
resText = "Empty"
}
console.log("text: ", text)
console.log("resText: ", resText)
res.send("Echo " + resText)
})
exports.api = functions.https.onRequest(app)
const isEmpty = (str: string): boolean => {
console.log("str: ", str, "+++")
const trimedStr = str.trim()
const result = (trimedStr === null || trimedStr === "")
console.log("result: ", result)
return result
}
構建以將打字稿轉換為javascript效果很好。 但是,當我執行POST方法時,發生以下錯誤。
> TypeError: Cannot read property 'trim' of undefined
> at isEmpty (/Users/kenny/Test/firebase_functions/functions/lib/index.js:22:27)
> at app.post (/Users/kenny/Test/firebase_functions/functions/lib/index.js:9:9)
> at Layer.handle [as handle_request] (/Users/kenny/Test/firebase_functions/functions/node_modules/expr
ess/lib/router/layer.js:95:5)
> at next (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/route.js:137:
13)
> at Route.dispatch (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/rou
te.js:112:3)
> at Layer.handle [as handle_request] (/Users/kenny/Test/firebase_functions/functions/node_modules/expr
ess/lib/router/layer.js:95:5)
> at /Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/index.js:281:22
> at Function.process_params (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/r
outer/index.js:335:12)
> at next (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/index.js:275:
10)
> at expressInit (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/middleware/in
it.js:40:5)
如何解決此錯誤?
首先,您需要查看為什么req.body.text為null或未定義的原因,然后進行如下所示的空檢查。
const isEmpty = (str: string): boolean => {
console.log("str: ", str, "+++")
const result = (!str || str.toString().trim() === ""); // converting toString because there chance body.text can be a number
console.log("result: ", result)
return result
}
您的問題是isEmpty
不准備接收不是字符串的內容。
快速修復:設置默認值
const isEmpty = (str=''): boolean => {
// code...
}
更好的解決方案 :驗證您的數據。
在開發任何API時,您需要驗證輸入數據以符合您的請求。
您正在使用API端點來創建新用戶,並且將需要一些數據以及請求,例如即將創建的用戶的名字,姓氏,年齡和生日。 顯然,將Sally作為年齡值或將53作為生日值傳遞不會使事情朝正確的方向發展。 您確實不希望不良數據通過您的應用程序傳播,所以您該怎么辦? 答案是數據驗證。 參考 。
我將使用Joi對此案例做一個簡單的例子:
// code...
const schema = {
text: Joi.string()
};
app.post('/', (req, res) => {
//my code starts here
const data = req.body;
const {error, value} = Joi.validate(data, schema);
if(error) {
return res.status(400).json(error);
}
// ends here
var resText: string = "NotEmpty"
const text: string = req.body.text
if (isEmpty(text)) {
resText = "Empty"
}
console.log("text: ", text)
console.log("resText: ", resText)
res.send("Echo " + resText)
});
// code...
只需檢查isEmpty
str
實際上為空,然后將if(!str) return true
即可。 它看起來像這樣:
const isEmpty = (str: string): boolean => {
console.log("str: ", str, "+++")
if(!str) return true;
const trimedStr = str.trim()
const result = (trimedStr === null || trimedStr === "")
console.log("result: ", result)
return result
}
在JavaScript中,真實值是在布爾上下文中遇到的真實值。 除非將它們定義為虛假,否則所有值都是真實的(即,除了false,0,“”,null,undefined和NaN外)。
那為什么不能像下面這樣使用?
if (!text) {
resText = "Empty"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.