Timestamp returns an error on BigQuery when sent by Firebase Functions

爱⌒轻易说出口 提交于 2019-12-24 09:57:34

问题


I'm trying to send timestamp field to BigQuery from Firebase Functions. I'm sending admin.firestore.FieldValue.serverTimestamp() and The field on BigQuery is TIMESTAMP type.

by i receive an error:

table.insert: {"errors":[{"errors":[{"message":"This field is not a record.","reason":"invalid"}]}],"response":{"kind":"bigquery#tableDataInsertAllResponse","insertErrors":[{"index":0,"errors":[{"reason":"invalid","location":"datetime","debugInfo":"","message":"This field is not a record."}]}]},"message":"A failure occurred during this request."}

My Code is:

exports.sessionsToBQ = functions.firestore
  .document("/Users/{userId}/Sessions/{sessionId}")
  .onWrite((change, context) => {

  const dataset = bigquery.dataset('App');
  const table = dataset.table('Sessions');
  tableName = 'Sessions'
  datasetName = 'App'

    dataset.exists().catch(err => {
      console.error(
        `dataset.exists: dataset ${datasetName} does not exist: ${JSON.stringify(
          err
        )}`
      )
      return err
    })

    //let table = dataset.table(tableName)
    table.exists().catch(err => {
      console.error(
        `table.exists: table ${tableName} does not exist: ${JSON.stringify(
          err
        )}`
      )
      return err
    })

    let row = {
      insertId: context.params.sessionId,
      json: {
        sessionId: context.params.sessionId,
        userId: context.params.userId,
        datetime: admin.firestore.FieldValue.serverTimestamp(),
        duration: change.after.data().duration,
        distance: change.after.data().distance,
        IP: change.after.data().IP
      },
    }

    return table.insert(row, { raw: true }).catch(err => {
      console.error(`table.insert: ${JSON.stringify(err)}`)
      return err
    })
  })

what I'm doing wrong? how to send correctly the current timestamp to big query?


回答1:


The issue resolved, By changing

admin.firestore.FieldValue.serverTimestamp()

with

Math.floor(Date.now() / 1000)


来源:https://stackoverflow.com/questions/49881487/timestamp-returns-an-error-on-bigquery-when-sent-by-firebase-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!