I'm trying to write a Firebase cloud function that triggers when an image is uploaded to a specific bucket. It sends that image over to document ai to be processed by a custom processor and logs the return in Firestore database.
I did the same thing with cloud vision and it worked fine. I just needed better parsing than Vision had to offer.
However, when I trigger this new cloud function I keep receiving an error in the Google logs. It started with Error:
3 INVALID_ARGUMENT: Request contains an invalid argument.
I added some logging to the code to try and narrow down the problem. eventually, I got this but it was a truncated response.
statusDetails: [ BadRequest { fieldViolations: [Array] } ]
Just to note I've checked and triple-checked my processor id, project id, and location. I've also checked my service account permissions.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { DocumentProcessorServiceClient } = require('@google-cloud/documentai').v1beta3;
admin.initializeApp({
credential: admin.credential.cert(require('./sample-room-58c38-bd767e088e9d.json')),
});
const client = new DocumentProcessorServiceClient({
projectId: 'sample-room-58c38',
keyFilename: './sample-room-58c38-bd767e088e9d.json',
});
const bucketName = 'sample-room-58c38-q6gtc';
exports.processImage = functions.storage
.bucket(bucketName)
.object()
.onFinalize(async (object) => {
const gcsUri = `gs://${bucketName}/${object.name}`;
const request = {
name: `projects/sample-room-58c38/locations/us/processor/a89f2d4fd6b09a9a`,
inputConfig: {
gcsDocumentUri: gcsUri,
mimeType: 'image/jpeg',
},
};
console.log('gcsUri:', gcsUri);
console.log('Request:', JSON.stringify(request, null, 2));
try {
const [result] = await client.processDocument(request);
const { document } = result;
const { formFields } = document;
const data = {};
formFields.forEach(field => {
const fieldName = field.fieldName.text;
const fieldValue = field.fieldValue.text;
data[fieldName] = fieldValue;
});
console.log('Data:', data);
return admin.firestore().collection('DocumentAI Photos').doc(object.name).set(data);
} catch (error) {
console.error('Error in processDocument:', JSON.stringify(error, null, 2)); // <--- Error logging here
throw error;
}
});