2
votes

I am trying to create collections and sub collections for document inside a collection dynamically from cloud functions, But I am getting following exception **

Argument "collectionPath" must point to a collection, but was "data/c2f7c4e84366". Your path does not contain an odd number of components

**

Logs -

Error: Argument "documentPath" must point to a document, but was "exports/user343434_inbox/profile". Your path does not contain an even number of components. at Firestore.doc (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/index.js:282:13) at admin.firestore.doc.set.then.ws (/user_code/lib/index.js:28:53)

code snippet: const p = admin.firestore().doc(exports/${userInboxCollectionName}/profile).set(reqData, { merge: false }) *

I am expecting cloud function to create the subcollection(userid_inbox) inside exports collection(already exists) dynamically if not exists and add the profile document.

1
Please edit your question to show the code that generates this error.Doug Stevenson

1 Answers

0
votes

Firestore works like this: collection / document / subCollection / "subDocument" / and so on.

You should try: (I'm using TS)

let p = admin.firestore().collection(`exports/${userUID}/${theSubcollection}`)
    .add({ message: messageString })
    .then(ref => {
        console.log('Added document with ID: ', ref.id)
});

//let's see the estructure here:
//collection.......exports   
//document.........${userUID}
//subcollection....${theSubcollection}
//document.........ref.id
//data.............message with value messageString

with this code you are creating an new document with the value message on a new subcollection.