10

I try to use Firestore in Cloud functions but I faced with error

db.collection(...).doc(...).collection(...).doc(...).add is not a function at Promise

I read these topics first, second and other. But id didn't help me. The package.json looks

"firebase": "^4.3.1",
"firebase-admin": "^5.5.1",
"firebase-functions": "^0.7.5",

One of cloud functions

const admin = require('firebase-admin');
var db = admin.firestore();

This code from function

const currentUserMatchProm = db.collection('userMatches').doc(currentUserID).collection('test').doc(matchID).add({
            'matchID': matchID,
            'matchedUserID': eventUserID,
            'timestamp': timestamp,
            'isActive': false
        });

        const eventUserMatchProm = db.collection('userMatches').doc(eventUserID).collection('test').doc(matchID).add({
            'matchID': matchID,
            'matchedUserID': currentUserID,
            'timestamp': timestamp,
            'isActive': false
        });

How can I fix it?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alexander Khitev
  • 6,417
  • 13
  • 59
  • 115

1 Answers1

21

doc is document, add function is only for collection. To write data to a document, use the set function:

doc(matchID).set({ ... })
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jaeseon Shin
  • 239
  • 2
  • 4
  • 1
    [Documentation says](https://firebase.google.com/docs/firestore/manage-data/add-data) "Behind the scenes, .add(...) and .doc().set(...) are completely equivalent, so you can use whichever is more convenient." but I do see where you _cannot_ do `.doc().add()` – Ronnie Royston Feb 18 '18 at 01:32