类型错误:无法读取零属性“长度”(TypeError: Cannot read property “

2019-10-17 08:00发布

我很新的JavaScript,并试图玩弄在谷歌电子表格和电子邮件的邮件合并功能。 我复制教程脚本,并做了一些必要的改变(至少是我能想到的)。 但是,当我试图运行该脚本,我得到了类型错误:无法读取零属性“长度”。 (线43)

上面提到的线43是用于以下循环。 是否有人可以帮助让我知道是固定的东西,所以我可以运行脚本?

// 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;
}

Answer 1:

如果没有对正则表达式匹配, templateVars将是无效的。 你需要你的循环之前检查此。

更新:

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


Answer 2:

我只是有这个同样的问题,但OP是有我不认为这个问题是一个涉及到的代码。

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

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

所不同的是" VS的一个是垂直的(作品),另一个是‘斜体’(不工作)

有人谁知道电脑格式的数据将如何能够解释这一点的重要性,而只是它打破了代码。



文章来源: TypeError: Cannot read property “length” from null