Firestore web code sample gives invalid argument type

若如初见. 提交于 2020-02-21 06:31:27

问题


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


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




回答2:


Quick workaround

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



回答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));



回答4:


Add a document

Then you can try this:

db.collection("cities").add({
    'name': "Tokyo",
    'country': "Japan"
})



回答5:


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



来源:https://stackoverflow.com/questions/46620162/firestore-web-code-sample-gives-invalid-argument-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!