如何解决Parquetjs库问题-异步错误?

时间:2019-02-28 18:29:00

标签: node.js parquet

我尝试使用parquetjs: https://www.npmjs.com/package/parquetjs

示例代码:

var parquet = require('parquetjs');

// declare a schema for the `fruits` table
var schema = new parquet.ParquetSchema({
  name: { type: 'UTF8' },
  quantity: { type: 'INT64' },
  price: { type: 'DOUBLE' },
  date: { type: 'TIMESTAMP_MILLIS' },
  in_stock: { type: 'BOOLEAN' }
});

// create new ParquetWriter that writes to 'fruits.parquet'
var writer = await parquet.ParquetWriter.openFile(schema, 'fruits.parquet');

// append a few rows to the file
await writer.appendRow({name: 'apples', quantity: 10, price: 2.5, date: new Date(), in_stock: true});
await writer.appendRow({name: 'oranges', quantity: 10, price: 2.5, date: new Date(), in_stock: true});

错误: SyntaxError: await is only valid in async function

应该是什么问题(因为他们将其发布为示例)?

1 个答案:

答案 0 :(得分:0)

SyntaxError: await is only valid in async function

这意味着您只能在函数签名中具有await关键字的函数中使用async关键字。

为快速解决,您可以使用async IIFE。

(async() => {
    // create new ParquetWriter that writes to 'fruits.parquet'
    var writer = await parquet.ParquetWriter.openFile(schema, 'fruits.parquet');

    // append a few rows to the file
    await writer.appendRow({name: 'apples', quantity: 10, price: 2.5, date: new Date(), in_stock: true});
    await writer.appendRow({name: 'oranges', quantity: 10, price: 2.5, date: new Date(), in_stock: true});
})()