建立对象/命名数组的键?

时间:2019-01-30 20:56:08

标签: javascript firebase firebase-realtime-database

我正在尝试将Firebase数据库与angolia服务器连接。我的Firebase数据库已同步到Excel工作表。我向他们求助,他们说:

您的“构建所有记录的数组以推送到Algolia”的代码将需要构建一个对象以推送到数组。您需要命名密钥,以使数据显示在Algolia中。

我不知道该怎么做。谁能帮我吗?

// Get all contacts from Firebase
database.ref('/trips').once('value', contacts => {
  // Build an array of all records to push to Algolia
  const records = [];
  contacts.forEach(contact => {
    // get the key and data from the snapshot
    const childKey = contact.key;
    const childData = contact.val();
    // We set the Algolia objectID as the Firebase .key
    childData.objectID = childKey;
    // Add object for indexing
    records.push(childData);
  });
  // Add or update new objects
  index
    .saveObjects(records)
    .then(() => {
      console.log('Contacts imported into Algolia');
    })
    .catch(error => {
      console.error('Error when importing contact into Algolia', error);
      process.exit(1);
    });
});

2 个答案:

答案 0 :(得分:0)

尝试一下:

# (untested)
save_kable_pdf_html = function(..., file) {
  kableExtra::save_kable(..., file = paste0(file, ".pdf"))
  kableExtra::save_kable(df, file = paste0(file, ".html"))
}

答案 1 :(得分:0)

在这种情况下,map方法可能会很有用。

const records = contacts.map(contact => {key: contact.key});

只需添加要显示在数据库中的密钥即可。