Error: Value for argument "data" is not a valid Firestore document. Detected an object of type "DocumentReference" that doesn't match the expected instance.

3,641 views
Skip to first unread message

MannepK

unread,
Jul 31, 2019, 11:19:19 AM7/31/19
to Firebase Google Group
Hello All,

I am trying to fetch a document from a collection using the document ID, which works by the way, and trying to save the document to a new collection. I have 2 requirements, depending on the user input either to use replace the document if the ID exists or create a altogether new document with new document id. Given below is my code.

item => input from HTTP Request body



let doc = firestore.collection(M_GLISTS_COLLECTION_NAME).doc(item.id);
if (item.replace === 'true') {
firestore.collection(cName).doc(item.id).set(doc.get());
} else {
firestore.collection(cName).add(doc);
}


I get the following error. Any help!

Error: Value for argument "data" is not a valid Firestore document. Detected an object of type "DocumentReference" that doesn't match the expected instance. Please ensure that the Firestore types you are using are from the same NPM package.)
    at validateDocumentData (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:608:15)
    at WriteBatch.set (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:9)
    at DocumentReference.set (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:330:14)
    at req.body.items.forEach.item (/srv/index.js:92:54)
    at Array.forEach (<anonymous>)
    at app.put (/srv/index.js:86:24)
    at Layer.handle [as handle_request] (/srv/node_modules/express/lib/router/layer.js:95:5)
    at next (/srv/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/srv/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/srv/node_modules/express/lib/router/layer.js:95:5)








Sam Stern

unread,
Jul 31, 2019, 11:56:15 AM7/31/19
to Firebase Google Group
HI there,

I think the problem is in this line:
firestore.collection(cName).doc(item.id).set(doc.get());

doc.get() is an asynchronous operation, so you can't pass it to set() right away.

You need to do one of the following:

Using a callback
doc.get().then((snap) =>
  firestore.collection(cName).doc(item.id).set(snap.data());
});

Using async/await
const snap = await doc.get();
firestore.collection(cName).doc(item.id).set(snap.data());

Let me know if that works!

- Sam


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/967c882a-48d9-41aa-835a-db2c914131d3%40googlegroups.com.

MannepK

unread,
Aug 6, 2019, 9:59:08 AM8/6/19
to Firebase Google Group
Hi Sam,

It worked. 

Now I am stuck at another place. Could you please suggest how to handle this? 
I have a collection C1 that has a Document D1 which has a sub collection SC1.
Now I want to add this Document D1 to Collection C2. The document D1 gets added to C2 but the sub collection SC1 doesn't get added to C2. Is there a straight forward way to do it or should I create a sub- collection manually and add the documents underneath the collection by iterating through the sub-collection?
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

Sam Stern

unread,
Aug 6, 2019, 12:08:35 PM8/6/19
to Firebase Google Group
Hi there,

Subcollections are not designed to be "attached" to their parent document so as you noticed they are not simple to move or delete.  If you really want to create an identical subcollection in two places you will have to iterate and copy each document.  Ideally you would find a data structure where this isn't necessary, but of course every app has its own requirements.

- Sam

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/e3c2242e-c54b-4b12-af08-ca391a9414be%40googlegroups.com.

MannepK

unread,
Aug 7, 2019, 8:19:47 PM8/7/19
to Firebase Google Group
Thank you! It worked. 
Reply all
Reply to author
Forward
0 new messages