Firebase firestore cloud functions showing Error: Invalid use of type “undefined” as a Firestore argument

Tags: , , ,



I have a project to add currency details into the firestore database and my project is doing with ionic 3

Whenever I add a new document to the collection a trigger function onCreate() will execute and update the document named ‘updated’.

But the trigger function always showing an error.

Error: Invalid use of type "undefined" as a Firestore argument.
    at Object.exports.customObjectError.val [as customObjectError] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:164:14)
    at Function.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:808:20)
    at Function.encodeFields (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:678:36)
    at Function.fromObject (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:218:55)
    at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/write-batch.js:291:39)
    at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:419:8)
    at Object.<anonymous> (/user_code/lib/index.js:28:10)
    at next (native)
    at /user_code/lib/index.js:7:71
    at __awaiter (/user_code/lib/index.js:3:12)

sombody please help..

i have spent lot of time on it.

Here is the code :

import * as functions from 'firebase-functions';

const admin = require('firebase-admin');
admin.initializeApp();

exports.createCurrency = functions.firestore
.document('Exchange/{ExchangeId}')
.onCreate( async (snap, context) => {

const id: string = snap.data().id;
const branchName: string = snap.data().branchName;
const currencyName: string = snap.data().currencyName;
const buyingRate : string = snap.data().buyingRate;
const sellingRate : string = snap.data().sellingRate;


 const newUser= admin.
 firestore()
 .doc(`Exchange/updated`)
 .set({
   id : id,
   branchName : branchName,
   currencyName : currencyName,
   sellingRate : sellingRate,
   buyingRate :buyingRate
  });

 return newUser;


 });

Answer

The error message is this:

Invalid use of type "undefined" as a Firestore argument.

You can see in your stack trace that this happens when you call set() with an object on a DocumentReference. It turns out that one of the values you’re passing in the object is undefined. Check each of the values that you’re passing and make sure all of them have an actual value:

 .set({
   id : id,
   branchName : branchName,
   currencyName : currencyName,
   sellingRate : sellingRate,
   buyingRate :buyingRate
  });

It’s impossible to tell which one it is from the error message, so you’ll have to print them all out do something to check each and every one of them.



Source: stackoverflow