Firestore web code sample gives invalid argument type

12,932

Solution 1

This is likely a cross-window issue: GWT code runs in an iframe; if Firebase is looking for a "bare object", it likely compares the object's constructor, which won't be the expected one if the object crosses the iframe boundary.

Maybe try using a new $wnd.Object() instead of {} or new Object().

Solution 2

Quick workaround

var city: any;
city = Object.assign({}, {
    name: "Tokyo",
    country: "Japan"
});
db.collection("cities").add(city)

Solution 3

I tried this code and it works with me if you have data comes as an Object from angular Form :

this.afs.collection("/products").add(Object.assign(product));
Share:
12,932
ML-Deluxe
Author by

ML-Deluxe

Updated on July 13, 2022

Comments

  • ML-Deluxe
    ML-Deluxe almost 2 years

    I am trying out the new Firestore by Firebase. When I run the code sample from https://firebase.google.com/docs/firestore/manage-data/add-data?authuser=0, I am getting an error.

    // Add a new document with a generated id.
    db.collection("cities").add({
        name: "Tokyo",
        country: "Japan"
    })
    .then(function(docRef) {
        console.log("Document written with ID: ", docRef.id);
    })
    .catch(function(error) {
        console.error("Error adding document: ", error);
    });
    

    Exception caught: (FirebaseError) : Function CollectionReference.add() requires its first argument to be of type object, but it was: a custom Object object

    Edit: Sorry I didnt mention I am using GWT and JSNI, it's working fine without gwt

    • Frank van Puffelen
      Frank van Puffelen over 6 years
      From a quick scan your code looks correct, which makes me wonder if some unprintable character was copied from the sample. Can you try typing the JSON object that you pass in, to see if that makes a difference?
    • ML-Deluxe
      ML-Deluxe over 6 years
      I typed it again, no difference
    • Frank van Puffelen
      Frank van Puffelen over 6 years
      Hmmm.... in that case I don't know. Let's hope someone else has a better idea.
    • Mel
      Mel about 4 years
      Did you find a solution to this problem? I have got the same issue.
  • Ruben
    Ruben over 6 years
    but why? why do we have to create a new object
  • AKASH CHAUHAN
    AKASH CHAUHAN over 6 years
    thanks, @thomas-broyer, it's because of the different envs