I am performing a simple operation where in im trying to increment the count of a field. the code is below:
import * as firebase from 'firebase';
const DocumentRef = db.doc(`pages/${request.params.pageId}`);
const increment = firebase.firestore.FieldValue.increment(1);
DocumentRef
.get()
.then(function (documentSnapshot) {
if(!documentSnapshot.exists){
return response.status(404).json({"message":"page not found"})
}else{
return db.collection(`ments`).add(newComment)
}
})
.then(()=>{
return DocumentRef.update({nComments: increment})
})
.then(() => {
response.status(200).json(newComment);
})
.catch((error) => {
console.log(error)
return response.status(500).json({message:error.code});
})
ending up in following error:
Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore document. Couldn't serialize object of type "FieldValueDelegate" (found in field "nComments"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
at WriteBatch.update (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:374:23)
at DocumentReference.update (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:377:14)
at screamDocumentRef.get.then.then (/srv/lib/handlers/screams.js:105:34)
at <anonymous>
The message is:
Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore document. Couldn't serialize object of type "FieldValueDelegate" (found in field "nComments"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
this error happens only when I add this "then" chain:
.then(()=>{
return DocumentRef.update({nComments: increment})
})
I do not understand why.
only relevant part of the schema, I've 2 collections.
collection: pages(nComments, body, etc) id: pageId
collection: ments(pageId, etc) id: mentId
when new ment document is added in ments collection, Im updating the count in pages collections document whose pageId is in ment documents pageId field.
nComments and pageId are fields.
I am performing a simple operation where in im trying to increment the count of a field. the code is below:
import * as firebase from 'firebase';
const DocumentRef = db.doc(`pages/${request.params.pageId}`);
const increment = firebase.firestore.FieldValue.increment(1);
DocumentRef
.get()
.then(function (documentSnapshot) {
if(!documentSnapshot.exists){
return response.status(404).json({"message":"page not found"})
}else{
return db.collection(`ments`).add(newComment)
}
})
.then(()=>{
return DocumentRef.update({nComments: increment})
})
.then(() => {
response.status(200).json(newComment);
})
.catch((error) => {
console.log(error)
return response.status(500).json({message:error.code});
})
ending up in following error:
Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore document. Couldn't serialize object of type "FieldValueDelegate" (found in field "nComments"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
at WriteBatch.update (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:374:23)
at DocumentReference.update (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:377:14)
at screamDocumentRef.get.then.then (/srv/lib/handlers/screams.js:105:34)
at <anonymous>
The message is:
Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore document. Couldn't serialize object of type "FieldValueDelegate" (found in field "nComments"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
this error happens only when I add this "then" chain:
.then(()=>{
return DocumentRef.update({nComments: increment})
})
I do not understand why.
only relevant part of the schema, I've 2 collections.
collection: pages(nComments, body, etc) id: pageId
collection: ments(pageId, etc) id: mentId
when new ment document is added in ments collection, Im updating the count in pages collections document whose pageId is in ment documents pageId field.
nComments and pageId are fields.
Share
Improve this question
edited Aug 3, 2020 at 15:57
asked Aug 3, 2020 at 15:41
user13178684user13178684
8
-
Please edit the question to show how you created the
db
variable. There should be enough information here so that anyone can copy the code and reproduce the issue. – Doug Stevenson Commented Aug 3, 2020 at 15:49 - @DougStevenson please check it out – user13178684 Commented Aug 3, 2020 at 16:00
- @DougStevenson the issue es only while adding return DocumentRef.update({nComments: increment}) – user13178684 Commented Aug 3, 2020 at 16:00
- Yes, I understand the problem. Please edit the question to answer my question above. How are you creating db? The code you show here needs to be plete. – Doug Stevenson Commented Aug 3, 2020 at 19:27
- @DougStevenson Ive manually created two collections, (pages and ments) and entered sample data, when I add a new ment document, I want to update count of ments inside a pages document. also should I do this via batch / transactions? since Im adding a document in one collection as well as updating count in another document from another collection? – user13178684 Commented Aug 4, 2020 at 3:43
1 Answer
Reset to default 11You're probably using admin
namespace to do the update while using increment
from the firebase
namespace. Try changing your increment statement to:
import * as admin from 'firebase-admin';
const increment = admin.firestore.FieldValue.increment(1);