Firebase FunctionsとExpressを使って、簡単なWebAPIを作成しています。
以下のようなソースコードをTypeScriptで作成し、ビルドをしてJavaScriptへの変換は問題なくできています。
TypeScript
1import * as functions from 'firebase-functions'; 2import * as express from 'express'; 3 4const app = express() 5 6app.post('/', (req, res) => { 7 var resText: string = "NotEmpty" 8 const text: string = req.body.text 9 10 if (isEmpty(text)) { 11 resText = "Empty" 12 } 13 console.log("text: ", text) 14 console.log("resText: ", resText) 15 res.send("Echo " + resText) 16}) 17 18exports.api = functions.https.onRequest(app) 19 20 21const isEmpty = (str: string): boolean => { 22 console.log("str: ", str, "+++") 23 const trimedStr = str.trim() 24 const result = (trimedStr === null || trimedStr === "") 25 console.log("result: ", result) 26 return result 27}
しかし、これをローカルサーバーにデプロイして、POST APIを呼び出したところ、以下のようなエラーとなりました。
> 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)
なぜか、Stringクラスのメソッドであるはずの、trim()
のところでエラーとなってしまいます。
同様に、match()
も利用することができませんでした。
アドバイスを頂けるでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/31 05:38