3

My current document looks like this:

{
    "assignmentName": "OS Assignment-2",
    "dueDate": "10 August",
    "createdAt": "2020-08-02T20:27:28.916Z",
    "maxMarks": 10,
    "facutyName": "Dr. Muskan Gupta",
    "semester": "3",
    "submittedBy": [
        {
            "studentName": "Test name",
            "downloadUrl": "URL"
        },
        {
            "downloadUrl": "URL3",
            "studentName": "Test name2"
        },
        {
            "downloadUrl": "https://downloadthisfile.com",
            "studentName": "Nakshatra Saxena"
        },
        {
            "studentName": "Hello Bortehr",
            "downloadUrl": "httpsav"
        }
    ],
    "program": "CSE",
    "subject": "Operating System with UNIX"
}

I want to push an object in the 'submittedBy' field but I get an error. The code I'm using right now is:

const submittedAssignment = {
    studentName: req.body.studentName,
    downloadUrl: req.body.downloadUrl,
    };
    admin
        .firestore()
        .collection("assignments")
        .doc(req.params.assignmentId)
        .update({
          submittedBy: firebase.firestore.FieldValue.arrayUnion(submittedAssignment)
        });
    })
    .then(() => {
      return res
        .status(201)
        .json({ message: `Assignment submitted successfully` });
    })
    .catch((err) => {
      console.error(err);
      res.status(500).json({ error: `Error submitting assignment` });
    });

But I'm getting this 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 "submittedBy"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).

2
  • 1
    Don't use images for text and code. I rolled back to the edit I made to fix the code using triple backtick code fences - I suggest learning how to use them. Commented Aug 2, 2020 at 21:33
  • Hey thanks for the edit. I'm new here, I'll learn to use that now. Thanks a lot. Commented Aug 3, 2020 at 12:12

1 Answer 1

8

You're mixing up Firestore SDKs. If you use the Admin SDK to make the update, you will also need to use the Admin SDK to specify FieldValue type values.

Instead of this:

      submittedBy: firebase.firestore.FieldValue.arrayUnion(submittedAssignment)

Use this:

      submittedBy: admin.firestore.FieldValue.arrayUnion(submittedAssignment)
Sign up to request clarification or add additional context in comments.

2 Comments

It works perfectly now. Thank you so much, I thought about this too earlier and changed it to admin.firestore(), another silly mistake. Thanks again!
Good to know. On Stack Overflow, it's customary to accept helpful answers as correct using the checkmark button on the left.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.