使用Sheet.js从Hapi.js服务器提供XLSX文件

时间:2019-09-18 11:48:49

标签: excel hapijs sheetjs

我有这样的路由,它从MongoDB提供对象数组:

{
  method: "GET",
  path: "/api/somepath",
  handler: (req, reply) => {
    readyResult = 0
    db
      .collection('someCollection')
      .find(query, function(findErr, result) {
        if (findErr) throw findErr;
        (async function() {
          readyResult = await result.toArray();
        })
      })
    return readyResult 
  }
}

此外,我有一个代码,可将这些数据作为excel文件导出到我的客户端上:

var ws = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'First');
XLSX.writeFile(wb, "sheetjs.xlsx");

我想要的是将excel生成的内容传输到服务器。我已经这样尝试过了:

{
  method: "GET",
  path: "/api/somepath",
  handler: (req, reply) => {
    readyResult = 0
    db
      .collection('someCollection')
      .find(query, function(findErr, result) {
        if (findErr) throw findErr;
        (async function() {
          readyResult = await result.toArray();
        })
      })
    var ws = XLSX.utils.json_to_sheet(readyResult);
    const wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'First');
    readyResult = XLSX.writeFile(wb, "sheetjs.xlsx");
    return readyResult 
  }
}

但是出了点问题:

TypeError: js.forEach is not a function
    at sheet_add_json (...\node_modules\xlsx\xlsx.js:20777:5)

0 个答案:

没有答案