- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在 Firebase 云函数中通过 TypeScript 实现一个简单的 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
}
将 typescript 转换为 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)
如何修复此错误?
最佳答案
你的问题是 isEmpty
没有准备好接收非字符串的东西。
快速修复:设置默认值
const isEmpty = (str=''): boolean => {
// code...
}
更好的解决方案:验证您的数据。
在开发任何 API 时,您需要验证您的请求的输入数据。
You are working on an API endpoint to create a new user and you will require some data along with the request like firstname, lastname, age and birthdate for the user you are about to create. Obviously, passing Sally as value for age or 53 for birthdate won't get things rolling in the right direction. You really don't want bad data making its way through your application so what do you do? The answer is Data Validation. Reference.
我将使用 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...
关于javascript - 如何修复 Node 中未定义错误的 "Cannot read property ' trim',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319829/
我认为 trim() 不存储 trim 后的值。 我在用户提交他们的字符串之前对结果应用了 trim 并且它起作用了,它通过删除额外的空间来正确显示。但是如果我取回 trim 后的值,它会给我用户输入
我遇到了一些困惑。 我知道 String 对象是不可变的。这意味着如果我从 String 类调用方法,例如 replace(),则 String 的原始内容不会改变。相反,一个新的 String 被返
下面是一个使用 trim 的简单示例。根据问题的标题,它们之间有什么区别吗?正如您在下面看到的,它们具有相同的输出。如果答案是“否”,使用哪个更好? 目前我使用 .trim() 因为这是我第一次看到
我从工作表中的网站复制了一些数据,注意到一些单元格除了预期值外,还包含尾随空格。 由于给我的代码带来麻烦,我想删除这些空格。我发现了一个有这个空间的特定单元格并尝试了: Worksheets(2).R
vba 中的 trim 和 trim$ 有什么区别?今天无意中在vba中使用left和trim函数时,编译器说找不到项目或库 当我用谷歌搜索它时,在一个论坛上我发现用户使用这样的方式 vba.tri
这个问题已经有答案了: Why does Date.parse give incorrect results? (11 个回答) 已关闭 4 年前。 我知道从字符串创建日期通常是一个坏主意,但这仍然引
jQuery trim 和原生 JavaScript trim 之间有什么区别吗? 是否有其他行为、安全、性能? 最佳答案 JavaScript .trim() 是在 ES 5.1 中定义的,不适用于
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
PowerShell 有一个非常好的内置帮助系统,我经常使用它,我可以通过 Get-Help * 查看所有帮助选项。并查询 Cmdlet 或可以使用 Get-Help about_* 查找主题然后说G
查看 jQuery 的源代码: // Use native String.trim function wherever possible trim: trim && !trim.call("\uFEF
如何 trim javascript 日期对象。我有一个日期,格式为(yyyy-d-m)。日期值为 2015-03-03,我想 trim 天数中的前 0,而不是月数。 示例:当我输入:2015-03-
喜欢听听您的意见和理由吗? 我听说深层目录不好,但它与下划线字符相比如何? 最佳答案 使用破折号 - Google 将破折号视为分隔词,但将下划线视为词的一部分。参见 http://www.codin
根据随附的示例工作表,TRIM 似乎无法正常工作,还是我误解了它? 无论哪种方式,目前对我来说非常令人沮丧,因为我有一个电子表格,我必须计算单元格中的单词数,并且由于这个问题,我无法建立一个完全有效/
在这个 jQuery 中,我可以绑定(bind)粘贴事件来验证表单字段,但是我在函数中的方法显然是错误的。我根本没有收到警报。 我只想修剪输入的文本并将其作为表单输入文本字段 #adsense_cli
我有: onclick="document.getElementById('field1').value = Math.round((parseFloat(document.getElementByI
我在页面上有一些小数点后 4 位的价格。有些产品的价格精确到小数点后 4 位,有些则只有 2 位。 目前,我们的网站设置为每种产品显示 4 位小数,我必须使用 JavaScript 来降低那些非 2
我有这棵“树”。 它是一个多维对象。每个“节点”最多有两个属性,分别是 data 和 children。 这是一个例子: {Account: { data: ['stuff'], children:
好吧,伙计们,要么是有一些小问题我没有发现,要么是该 sleep 了。不管是什么,我都非常感谢您的建议。 我正在用 PHP 生成一个 XML 文件。一切都很顺利,直到我输入这一行: $out .= "
我有一个从 AJAX 返回的变量 row['case'] ,它等于“Hello World!”。问题是,当我尝试将其分配给另一个变量时,只有“Hello”被分配,row['case'] =“Hello
我想知道是否可以将整数转换为字符串并在一步内对其进行 trim 。 我有以下对象: 当前表单 { id:1, name:"Abusicus", value:12203, period:"Morning
我是一名优秀的程序员,十分优秀!