0
votes

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)
    }
  })
1

1 Answers

0
votes

When you call JSON.stringify you get back a string. And while a string is a valid value for a field in Firestore, you're trying to set the entire document to a string which isn't possible:

return docRef.set(JSON.stringify(fullTextAnnotation.text))

You'll want to set a specific field to this value:

return docRef.set({ text: JSON.stringify(fullTextAnnotation.text) })