首页 > 解决方案 > 如何在 Node.js 中将 BigQuery 表数据放入 Dropdown

问题描述

我是 Javascript 的新手。我一直在使用 Node.js 中的 BigQuery 表数据制作动态下拉列表。我已经从 BigQuery 获得了表数据并制作了静态下拉列表,但我不知道如何将表数据放入下拉列表中。有人帮我吗??对不起我糟糕的代码。

index.js


const {BigQuery} = require('@google-cloud/bigquery');
  
const bigquery = new BigQuery({
    projectId: 'test_project',
});

bigquery
    .dataset("test_table")
    .getTables()
    .then(results => {
        const tables = results[0];
        tables.forEach(table => console.log(table.id));
    })
    .catch(err => {
        console.error('ERROR:', err);
    });

const list = ["I want to put table data(tables) in here"];
// select tag
let slt = document.getElementById("slt");
addTables(slt);

// return data 
function getList() {
  return new Promise(function (onFulliflled, onRejected) {
    onFulliflled(list);
  });
}

function addTables(slt) {
  getList()
    .then((list) => {
      for (item of list) {
        // make option
        let option = document.createElement("option");
        option.text = item;
        option.value = item;

        // add option
        slt.appendChild(option);
      }
    })
    .catch((err) => {
      console.error("error", err);
    });
}

索引.html


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>sample</title>
  </head>
  <body>
      <h3>Please choose table</h3>
      <select id="slt" name="slt"></select>
      <script src="./index.js"></script>
  </body>
</html>

标签: javascriptnode.jsgoogle-bigquery

解决方案


我用下面的代码解决了这个舞会

function listTables(callback) {
    //...

        .then(results => {
            const tables = results[0];
            tables.forEach(table => list.push(table.id));
            callback(list);
        })
    //...
}

listTables(function(list){

});


推荐阅读