21

I am getting the error

Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object.

when updating a document, using firebase admin SDK. Here the Typescript code.

var myDoc = new MyDoc();
myDoc.Public.Name = "Jonh Doe" //setup up content

admin.firestore()
     .collection('MyDocs')
     .doc("Id1")
     .set(myDoc);

4 Answers 4

6

In case someone else bump into the same issue, the solution is to simply use JSON to instantiate the object, like this:

var myDoc = {
        Public: {
            Name: "Jonh Doe"
        }
    } as MyDoc; //keep type to still get typescript compiler validations
Sign up to request clarification or add additional context in comments.

Comments

5

I did something similar:

var myDoc = <MyDoc> {
    Public: {
        Name: "Jonh Doe"
    }
}

It is semantically the same, I just think it is a bit cleaner.

Comments

2

I had same problem, in my case I'd forgot to add Content-Type:application/json to my header when sending request, and then the object was treated as string and I got that error.

Comments

2

You can recreate js object via; {...__data}

return refDB.set({...__data}).then((newData) => {

})

3 Comments

Can you more specific? What you get?. This is modern approach, maybe your nodejs version is not enough.
To be fair, I was using js instead of ts, which might have been it. However, I ended up using update() instead like this this.firestore.collection('influencers').doc(influencerDoc.id).update(data)
main issue for this error, newly created object has some prototype which is hidden element of array will verifiying from firebase library or api. If you created new object and pass data, problem partialy solved. But i didn't go deeper. So i don't have exact answer.

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.