【发布时间】:2023-03-28 23:44:01
【问题描述】:
我一直在尝试创建一个函数,将输入的内容添加到 Firestore 集合文档,然后为该文档创建一个子集合,获取其余内容并将其放入该子集合中的文档中。但是,我遇到了一个问题,我可以添加到集合中,但不能创建并添加到子集合中。有没有什么办法解决这一问题?我的代码如下。
document.getElementById("submitButton").addEventListener("click", function() {
var storyTitle = document.getElementById("storyTitle").value;
var chapterTitle = tinymce.get("writeTitle").getContent();
var chapterContent = tinymce.get("chapterContent").getContent();
const db = firebase.firestore();
addStory(storyTitle,chapterTitle,chapterContent);
});
function addStory(story,chapter,theContent) {
const db = firebase.firestore();
const sub = db.collection("Stories").doc(story).collection("Chapters");
//const docRef = db.collection("Stories").doc(storyTitle);
document.getElementById("testing").innerHTML = story + ", " + chapter + ", " + theContent;
db.collection("Stories").doc(story).set({
title: story
}).then(() => {
console.log("Document successfully written!");
}).catch((error) => {
console.error("Error writing document: ", error);
});
db.collection("Stories").document(story).collection("Chapters").add({
title: chapter,
content: theContent
}).then(() => {
console.log("Document successfully written!");
}).catch((error) => {
console.error("Error writing document: ", error);
});
}
【问题讨论】:
标签: database firebase google-cloud-firestore