我正在尝试将我在表单中输入的数据设置为 Firebase 中的字段名称。$("#blogUID").val()它在第 3 行(博客/blogUID 集合)中工作得很好。但是,当我尝试在 Stats/Blogs 集合(第 10 行)中设置字段名称时,出现以下错误:error.ts:166 Uncaught FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined at new ui (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:52471) at Vh (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:164122) at kh (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:163328) at _d.doc (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:280502) at HTMLButtonElement.<anonymous> (http://localhost:8080/TeamSolheim_Page1.html:251:36) at HTMLButtonElement.dispatch (https://code.jquery.com/jquery-3.5.1.min.js:2:43090) at HTMLButtonElement.v.handle (https://code.jquery.com/jquery-3.5.1.min.js:2:41074)1 $("#blog-submit-button").click(function(){2 // Add a new document in collection3 // db.collection("Blogs").doc($("#blogUID").val()).set({4 // blogDate: $("#blogDate").val(),5 // blogLink: $("#blogLink").val(),6 // blogTitle: $("#blogTitle").val(),7 // })8 // .then(function() { 9 db.collection("Stats").doc($("Blogs").val()).set({10 $("#blogUID").val(): $("#blogLikes").val(),11 })12 .then(function() { console.log("Document successfully written!"); $("#blog-form").hide(); $("#create-form").show(); }) .catch(function(error) { console.error("Error writing document 2: ", error); }); // }) // .catch(function(error) { // console.error("Error writing document 1: ", error); // }); })权限对任何经过身份验证的用户都是开放的: match /Stats/Blogs { allow read; allow write: if request.auth != null; }
2 回答

小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
向下移动到格式化的答案:
.set() 需要一个对象——你在第一次尝试时就把它“靠近”了。尝试这个:
const blogger = $("#blogUID").val() // jQuery
const bloggerLikes = $("#blogLikes").val() // jQuery
db.collection("Stats").doc("Blogs").set( { [ blogger]: bloggerLikes } )
你需要[ ] 来使用 blogger 值作为索引/字段名

函数式编程
TA贡献1807条经验 获得超9个赞
从堆栈跟踪来看,您的错误似乎与您尝试在第 9 行查询文档的方式有关。从第 3 行开始,正确db.collection("Blogs").doc($("#blogUID").val()).set()
搜索形式blogUID
,其中基于的查询id
似乎是有效的。
但是,在您的第 9 行中,这db.collection("Stats").doc($("Blogs").val()).set()
不会返回基于 an 的查询,因为您可以注意到函数中和id
之间的区别。由于错误通知问题在未定义范围内,请更改您在第 9 行中调用的方式,以遵循第 3 行中的相同模式。#blogUID
Blogs
.doc()
.doc()
添加回答
举报
0/150
提交
取消