[英]Uncaught TypeError: firebase.firestore()…set is not a function at HTMLButtonElement.<anonymous>
I use Firebase to develop a web app.我使用 Firebase 来开发 web 应用程序。 I got this error message我收到此错误消息
Uncaught TypeError: firebase.firestore(...).collection(...).orderBy(...).limit(...).set is not a function
at HTMLButtonElement.<anonymous> (main.js:152)
because of this code.因为这个代码。
firebase.firestore().collection('script').orderBy('timestamp','desc').limit(1).set({
finaltext: resultText.value,
name: firebase.auth().currentUser.displayName,
uid: firebase.auth().currentUser.uid
},{merge: true}).catch(function(error){
console.error('Error writing new message to Firestore', error);
});
I searched the Internet, but couldn't find why this error message occurred.我搜索了互联网,但找不到出现此错误消息的原因。 Could you give me any advice, please?请给我一些建议好吗?
The method limit
returns a value of type Query
:方法limit
返回Query
类型的值:
limit限制
limit ( limit: number ): Query < T >
Creates and returns a new Query that only returns the first matching documents.创建并返回一个只返回第一个匹配文档的新查询。
https://firebase.google.com/docs/reference/js/firebase.firestore.Query#limit https://firebase.google.com/docs/reference/js/firebase.firestore.Query#limit
Therefore both orderBy
and limit
are used when you want to retrieve data.因此,当您要检索数据时,会同时使用orderBy
和limit
。 If you want to add data to the database then just do the following:如果要向数据库添加数据,只需执行以下操作:
firebase.firestore().collection('script').set({
finaltext: resultText.value,
name: firebase.auth().currentUser.displayName,
uid: firebase.auth().currentUser.uid
},{merge: true}).catch(function(error){
console.error('Error writing new message to Firestore', error);
});
You need to first fetch the desired document, with get()
, before writing the new field(s) with set()
(with merge option) or update()
.在使用set()
(使用合并选项)或update()
编写新字段之前,您需要先使用get()
获取所需的文档。
firebase.firestore().collection('script').orderBy('timestamp','desc').limit(1).get()
.then(querySnapshot => {
if (!querySnapshot.empty) {
//We are sure the document exists, we can then use update()
return querySnapshot.docs[0].ref.update({
finaltext: resultText.value,
name: firebase.auth().currentUser.displayName,
uid: firebase.auth().currentUser.uid
});
} else {
throw new Error("No document");
}
})
.catch(error => {
console.log("Error:", error);
});
Note that the get()
method returns a QuerySnapshot
, so you need to do querySnapshot.docs[0]
to get the DocumentSnapshot
corresponding to the unique doc returned by the query.请注意, get()
方法返回一个QuerySnapshot
,因此您需要执行querySnapshot.docs[0]
以获取与查询返回的唯一DocumentSnapshot
对应的 DocumentSnapshot。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.