“将圆形结构转换为JSON”来自Cloud Function NodeJ的BigQuery插入

问题描述 投票:0回答:1

我正在尝试将NodeJs中编写的Cloud Function的json对象数组加载到BigQuery Table。我收到错误消息“尽管没有任何循环引用,但仍将循环结构转换为JSON。我的代码如下。

我有一个可以调用的函数

var num_rows = insertRowsAsStream(data);

res.status(200).send(num_rows);

inserRowsAsStream函数为

async function insertRowsAsStream(data) {
  // Inserts the JSON objects into my_dataset:my_table.

  // Create a Client
  const bigQuery = new BigQuery({projectID: 'myProject'});

  const datasetId = 'test_data';
  const tableId = 'temp';
  const rows = data;

  // Insert data into a table
  await bigQuery
    .dataset(datasetId)
    .table(tableId)
    .insert(rows);

  return rows.length ;
}

传递给insertRowsAsStream的数据arg的格式。任何地方都没有循环引用。


[ {"name": "John Doe", "age": 30, "title": "Director"}, {"name": "Jane Doe", "age": 27, "title": "Manager"} ]

javascript node.js google-bigquery google-cloud-functions
1个回答
0
投票

虽然您的函数似乎正在返回rows.length,但实际上它正在返回一个可解决为rows.length值的Promise。这是因为insertRowsAsStream被声明为async,并且所有async函数都返回一个Promise。

您应该做的也是await insertRowsAsStream的结果,假设包含的函数也是async

var num_rows = await insertRowsAsStream(data);
res.status(200).send(num_rows);

您可能还想考虑发送数字以外的内容。也许调用者可以轻松解析的JSON对象:

res.json({ numRows: num_rows })

这将使您通过添加新属性来更改响应,而不会破坏现有客户端。

© www.soinside.com 2019 - 2024. All rights reserved.