admin管理员组文章数量:1279186
I am trying out the new Firestore by Firebase. When I run the code sample from , 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
I am trying out the new Firestore by Firebase. When I run the code sample from https://firebase.google./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
Share Improve this question edited Oct 14, 2017 at 7:06 ML-Deluxe asked Oct 7, 2017 at 12:32 ML-DeluxeML-Deluxe 231 gold badge1 silver badge5 bronze badges 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? – Frank van Puffelen Commented Oct 7, 2017 at 14:01
- I typed it again, no difference – ML-Deluxe Commented Oct 7, 2017 at 14:16
- Hmmm.... in that case I don't know. Let's hope someone else has a better idea. – Frank van Puffelen Commented Oct 7, 2017 at 15:10
- Did you find a solution to this problem? I have got the same issue. – Mel Commented Jan 25, 2020 at 21:03
5 Answers
Reset to default 3This is likely a cross-window issue: GWT code runs in an iframe; if Firebase is looking for a "bare object", it likely pares 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()
.
Quick workaround
var city: any;
city = Object.assign({}, {
name: "Tokyo",
country: "Japan"
});
db.collection("cities").add(city)
I tried this code and it works with me if you have data es as an Object from angular Form :
this.afs.collection("/products").add(Object.assign(product));
Add a document
Then you can try this:
db.collection("cities").add({
'name': "Tokyo",
'country': "Japan"
})
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..
本文标签: javascriptFirestore web code sample gives invalid argument typeStack Overflow
版权声明:本文标题:javascript - Firestore web code sample gives invalid argument type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741255891a2366664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论