Firestore web code sample gives invalid argument type
Asked Answered
J

5

2

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

Jellaba answered 7/10, 2017 at 12:32 Comment(4)
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?Comical
I typed it again, no differenceJellaba
Hmmm.... in that case I don't know. Let's hope someone else has a better idea.Comical
Did you find a solution to this problem? I have got the same issue.Varrian
K
3

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().

Kyser answered 22/10, 2017 at 12:4 Comment(0)
P
3

Quick workaround

var city: any;
city = Object.assign({}, {
    name: "Tokyo",
    country: "Japan"
});
db.collection("cities").add(city)
Putrescible answered 8/10, 2017 at 5:22 Comment(2)
but why? why do we have to create a new objectCripple
thanks, @thomas-broyer, it's because of the different envsPutrescible
K
3

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().

Kyser answered 22/10, 2017 at 12:4 Comment(0)
G
1

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));
Gand answered 17/9, 2018 at 12:19 Comment(0)
V
0

Add a document

Then you can try this:

db.collection("cities").add({
    'name': "Tokyo",
    'country': "Japan"
})
Veer answered 21/10, 2017 at 1:13 Comment(0)
J
0

Seems like GWT is producing some custom object like the error says, so as workaround I am using a non JSNI convert method function to create objects like this

function convertObject(data) {
    var obj = {}
    Object.keys(data).forEach(function(key,index) {
        console.log(key);
        obj[key] = data[key];
    });
    return obj;
}

Still just a workaround and would love to know if there is a better solution..

Jellaba answered 22/10, 2017 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.