问题
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