Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore document add gives error "Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value"

I'm working on a react project and this is my very first react project. This code is deployed successfully. But get some errors while testing with postman. I "post" the "createScream" functions's URL and send it. then I got this errors.

Any help towards resolution is highly appreciated. I'm new to react world. Here's my index.json file

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
 exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello world");
 });

 exports.getScreams = functions.https.onRequest((req, res)=> {
     admin
     .firestore()
     .collection('screams')
     .get()
     .then(data=>{
         let screams =[];
         data.forEach(doc =>{
             screams.push(doc.data());
         });
         return res.json(screams);
     })
     .catch((err)=> console.error(err));
 });

 exports.createScream = functions.https.onRequest((req, res)=> {
    const newScream = {
        body:req.body.body,
        userHandle: req.body.userHandle,
        createdAt: admin.firestore.Timestamp.fromDate(new Date())
    };

    admin
    .firestore()
    .collection('screams')
    .add(newScream)
    .then((doc)=>{
        res.json({message:'document ${doc.id} created successfully'});
    })
    .catch((err)=>{
        res.status(500).json({ error:'something went wrong'});
        console.error(err);

    });

I got this error message

Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field body).
    at Object.validateUserInput (E:\survival\TF2\functions\node_modules\@google-cloud\firestore\build\src\serializer.js: 273: 15)
    at Object.validateDocumentData (E:\survival\TF2\functions\node_modules\@google-cloud\firestore\build\src\write-batch.js: 611: 22)
    at CollectionReference.add (E:\survival\TF2\functions\node_modules\@google-cloud\firestore\build\src\reference.js: 1765: 23)
    at exports.createScream.functions.https.onRequest (E:\survival\TF2\functions\index.js: 38: 6)
    at Run (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 608: 20)
    at C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 582: 19
    at Generator.next (<anonymous>)
    at C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 7: 71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 3: 12)
like image 348
Erandi Dilshani Avatar asked Sep 15 '19 12:09

Erandi Dilshani


1 Answers

To solve this issue, use the ignoreUndefinedProperties setting on the Firestore instance.

This skips undefined properties during serialization so that they don't get written into the Firestore document in the first place:

import * as admin from 'firebase-admin';

admin.initializeApp();

const firestore = admin.firestore();
firestore.settings({ ignoreUndefinedProperties: true });
const doc = firestore.doc(`demo`);
doc.create({ id: 1, description: undefined }); // works without exception
like image 71
Mobiletainment Avatar answered Sep 30 '22 07:09

Mobiletainment