javascript 类型错误:无法从 null 读取属性“length”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12949618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:30:04  来源:igfitidea点击:

TypeError: Cannot read property "length" from null

javascriptnull

提问by user1755506

I'm very new with javascript and trying to play around with Mail Merge function in Google spreadsheet and mail. I copied the tutorial script and made some necessary changes (at least what I can think of). But when I tried to run the script, I got TypeError: Cannot read property "length" from null. (line 43)

我对 javascript 非常陌生,并试图在 Google 电子表格和邮件中尝试使用邮件合并功能。我复制了教程脚本并进行了一些必要的更改(至少是我能想到的)。但是,当我尝试运行该脚本时,出现了 TypeError:无法从 null 读取属性“长度”。(第 43 行)

The line 43 mentioned above is the for loop below. Can someone please help let me know what to be fixed so I can run the script?

上面提到的第 43 行是下面的 for 循环。有人可以帮助我知道要修复什么,以便我可以运行脚本吗?

// Replaces markers in a template string with values define in a JavaScript data object.
// Arguments:
//   - template: string containing markers, for instance ${"Column name"}
//   - data: JavaScript object with values to that will replace markers. For instance
//     data.columnName will replace marker ${"Column name"}
// Returns a string without markers. If no data is found to replace a marker, it is
// simply removed.
function fillInTemplateFromObject(template, data) {
    var email = template;
    // Search for all the variables to be replaced, for instance ${"Column name"}
    var templateVars = template.match(/$\{\"[^\"]+\"\}/g);
    // Replace variables from the template with the actual values from the data object.
    // If no value is available, replace with the empty string.
    for (var i = 0; i < templateVars.length; ++i) {
        // normalizeHeader ignores ${"} so we can call it directly here.
        var variableData = data[normalizeHeader(templateVars[i])];
        email = email.replace(templateVars[i], variableData || "");
    }
    return email;
}

回答by Barmar

If there were no matches for the regular expression, templateVarswill be null. You need to check for this before your loop.

如果正则表达式没有匹配项,则为templateVars空。您需要在循环之前检查这一点。

UPDATE:

更新:

if (templateVars !== null) {
    for (var i = 0; i < templateVars.length; i++) {
        ...
    }
}

回答by mrax

I just had this same issue but the issue the OP is having I don't think is one that relates to the code.

我刚刚遇到了同样的问题,但 OP 遇到的问题我认为与代码无关。

It is the formatting of the email template offered by Google in their tutorial.

这是谷歌在他们的教程中提供的电子邮件模板的格式。

The placeholder is ${"First Name"}however depending on how you edit these you can get ${“First Name”}which is quite different

${"First Name"}然而,占位符取决于你如何编辑这些你可以获得${“First Name”}这是完全不同的

The difference is the "vs the One is vertical (works), the other is "italicised" (doesn't work)

区别在于"vs One 是垂直的(有效),另一个是“斜体”(无效)

Someone who knows about how computers format data will be able to explain the importance of this, but simply it breaks the code.

了解计算机如何格式化数据的人将能够解释这一点的重要性,但它只是破坏了代码。