I am using Google Vision API and a firebase cloud function for some handwritten text documents that are then stored into Firestore.
I am following the "Detect handwriting in images" guide here: https://cloud.google.com/vision/docs/handwriting#detect_document_text_in_a_remote_image
The problem I am having is that I can't seem to store the text payload into firestore. I can see the data coming through in the Cloud logs, but then I get an error:
imageTagger.js cloud function:
error in this function Error: Value for argument "data" is not a valid Firestore document. Input is not a plain JavaScript object.
Here is my cloud function. Anyone spot anything?
imageTagger.js
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as vision from '@google-cloud/vision'
// Instantiate a vision client
const client = new vision.ImageAnnotatorClient()
const bucketName = 'demobucket'
export const imageTagger = functions.storage
.bucket(bucketName)
.object()
.onFinalize(async (object) => {
// return console.log('objectmetadata: ', object)
try {
const fileName = object.name
const imageUri = `gs://${bucketName}/${fileName}` // format required to send imageURLs to Google Vision
const [result] = await client.documentTextDetection(imageUri)
const fullTextAnnotation = result.fullTextAnnotation
const docRef = admin.firestore().collection('photosText').doc('test')
return docRef.set(JSON.stringify(fullTextAnnotation.text))
} catch (error) {
return console.error('error in this function', error)
}
})