【问题标题】:function calls the var as undefined函数将 var 称为未定义
【发布时间】:2019-01-25 19:47:17
【问题描述】:

我的 javascript 代码中有这个功能:

function fillTemplate({ table_name, id }) {
  console.log("ID OF TABLES  " + table_name);
  return `
  <div class="noten_tabelle_permission" id="noten_tabelle_permission">
    <h1 id="member_name">${id}</h1>
    <table id="${table_name}" style="width:100%">
      <tr>
        <th>Fach</th>
        <th>mündlich</th>
        <th>Klausur</th>
      </tr>
      <!-- Make content with js code -->
    </table>
  </div>
  `;
}   

当我用这个(例如)fillTemplate("grade_table" + table_number, doc.id); 调用这个函数并将 table_name 打印到控制台时,它是未定义的。为什么以及如何解决此问题?
当您需要整个代码来理解或修复步骤时,这里是整个 sn-p:

db.collection("classes").doc(data.readGrades).get().then(function(doc) {
    if (doc.exists) {
        const data = doc.data();
        const members = data.members;

        members.reduce((chain, el) => {

          table_number++;

          const html = fillTemplate("grade_table" + table_number, doc.id);
          document.getElementById("main_padding").insertAdjacentHTML('beforeend', html);

            return chain.then(() =>
                db.collection("users").doc(el).collection("grades").get().then(function(querySnapshot) {
                    querySnapshot.forEach(function(doc) {
                        const data = doc.data();

                        addToTable("grade_table" + table_number, doc.id, data.mdl, data.klu);
                    });
                })
            )
        }, Promise.resolve());
    } else {
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});  

【问题讨论】:

  • fillTemplate({ table_name, id })更改为fillTemplate( table_name, id )

标签: javascript html google-cloud-firestore undefined


【解决方案1】:

在您的 fillTemplate() 函数中,您正在请求一个对象 {}

你需要替换:-

function fillTemplate({ table_name, id }) {

function fillTemplate(table_name, id) {

这应该可以解决您遇到的问题。

【讨论】:

  • 补充一点,当你的函数定义中有大括号时,你实际上是在将单个参数破坏为变量table_nameid。如果您希望使用两个参数,您可以在此答案中进行建议的更改。或者,您可以更改函数调用以传入对象fillTemplate({ table_name: 'my table name', id: 'myID' })
猜你喜欢
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2017-09-14
  • 2016-05-08
  • 2012-05-31
  • 2018-11-02
相关资源
最近更新 更多