主题标题可能会令人困惑。我有 students
子项下的用户。此外,我还有 mathlab
子项。我想要做的是在 matlab
子项下获取每个学生的 id,并在 students
子项下添加一些特定的子项。
因此,如果 mathlab 中的学生在 students/id/
下创建 "class:"math"、 ps:"A"。
这是 firebase 结构。
{
"mathlab" : {
"8SFX6ozygaeYm5s80XwEH15eOvR2" : {
"opos" : "3395762152383824847.5408566913692403"
},
"FouXbMWlPpQfkAHSJ98cRyMGyMq2" : {
"opos" : "2408503147879014222.4051320981527633"
},
"pAB5SPbZ42RkpeyB1vSe7vMgh0V2" : {
"opos" : "4282105147184071736.7786257557636794"
}
},
"students" : {
"3fw0j4tY1jMuugWaq9N1DZSPNBh2" : {
"opos" : "4316375953600111482.6297165447082696"
},
"8SFX6ozygaeYm5s80XwEH15eOvR2" : {
"opos" : "3395762152383824847.5408566913692403"
},
"FouXbMWlPpQfkAHSJ98cRyMGyMq2" : {
"opos" : "2408503147879014222.4051320981527633"
},
"UCQanT45dlWDb2ntFITa33pC4h22" : {
"opos" : "5279089148868706990.4495067048309678"
},
"aVoyqW6IcsdJEylwT7uuIyYU87l1" : {
"opos" : "4668496806381405007.7330624192123722"
},
"pAB5SPbZ42RkpeyB1vSe7vMgh0V2" : {
"opos" : "4282105147184071736.7786257557636794"
},
"uDQ7ISmmG7RsbCizNXQIZvysEK22" : {
"opos" : "1342746957828245395.2663129729855386"
},
"xM9U9MjoZUVOcqOVG38SzLrzd1z1" : {
"opos" : "3998977600021296207.4092046127102224"
}
}
}
到目前为止我做了什么
我收集了学生的每个 ID 并推送到一个数组中。但我如何一次性创建 child 呢?
var mathkids = [];
var matstudens1;
myref.child('mathlab').on("value", function(snapshot) {
snapshot.forEach(function(data) {
//Get id's
mathkids.push(data.key);
});
console.log(mathkids);
});
}
这应该是这样的。
|students
|8SFX6ozygaeYm5s80XwEH15eOvR2
|class:"math"
|ps:"A"
|5pw4MkVxKob8nXiHojTl61tQQ822
|class:"math"
|ps:"A"
|FeP6H8H0PyVjMDcqwH3zbY0xAxg2
...
|mathlab
|8SFX6ozygaeYm5s80XwEH15eOvR2
|5pw4MkVxKob8nXiHojTl61tQQ822
...
更新
因此,我获取了学生 ID 并将其存储到“mathkids”数组中。这是我推送数据时的操作方法。
secondref = firebaseRef = firebase.database().ref().child("students").child(matstudens1);
secondref.on('value' ,function(datasnapshot) {
secondref.child("class").set("math");
secondref.child("ps").set("A");
});
}
每次我必须单击按钮更改“matstudens1”值。我如何立即为所有“数学 child ”使用?
最佳答案
我不得不说我仍然不太确定你想要实现什么目标。但是,我认为您正在尝试执行一次性事务或原子更新,在一次调用中添加/更新数据库。然后,如果出现问题,整个事务就会失败,否则一切都会成功......以维护数据库完整性。
因此,假设我是对的,这里是一个 node.js 云函数(由其他函数调用,因此不会自行导出),我用它从 Firebase RTDB 中删除记录或节点。关键是它是一个“原子更新”,因此要么全部有效,要么全部失败......保持数据完整性。在我使用“null”删除记录的地方,您可以使用“math”、“A”等来向适当的子项添加或更新记录。您只需为这些 child 设置正确的引用即可。然后,单个语句 await databaseRoot.update(atomicUpdate);
执行整个事务。虽然它是“更新”,但会创建任何不存在的记录。您应该能够从代码片段中看到它是如何工作的,即使它不是特定于您的数据库的。然后,您需要弄清楚如何从数组中填充atomicUpdate
。
希望我正确地猜到了你的目的,这会有所帮助。 :-)
async function deleteChatThreadStructure(authenticatedUserId, participantId) {
var databaseRoot = db.ref();
var chatThreadId = setChatThreadId(authenticatedUserId, participantId);
var chatsNodeKey = "/chats/" + chatThreadId;
var chatsParticipantsNodeAuthUserKey = "/chatsParticipants/" + authenticatedUserId + "/" + participantId;
var atomicUpdate = {
[chatsNodeKey]: null,
[chatsParticipantsNodeAuthUserKey]: null,
};
try {
await databaseRoot.update(atomicUpdate);
console.info("Chat Thread Structure: " + chatThreadId + " deleted!");
}
catch (error) {
console.error("Remove Chat Thread Structure " + chatThreadId +
" failed with " + error.message);
}
}
更新: 当我查看您的“它必须是什么样子”时,“mathlab”下的列表只是没有值或子项的键。您无法在需要键值对的 Firebase 中执行此操作。因此,您可以将每个键存储为相应的“true”值,以使它们成为 k/v 对,然后可以单独搜索/访问它们,或者我认为您可以将数组存储为键“mathlab”的值。不过我自己还没有尝试过。
进一步更新: 我决定尝试一下。以下代码生成了此数据库结构。您需要选择您喜欢的方式以及您希望如何处理更新和删除。
const List<String> students = [
"Student 1",
"Student 2",
"Student 3",
"Student 4",
"Student 5",
"Student 6",
"Student 7",
"Student 8",
];
_atomicUpdate.addAll(
{
"/root/mathLabV1": students,
"/root/mathLabV2/${students[0]}": true,
"/root/mathLabV2/${students[1]}": true,
"/root/mathLabV2/${students[2]}": true,
"/root/mathLabV2/${students[3]}": true,
"/root/mathLabV2/${students[4]}": true,
"/root/mathLabV2/${students[5]}": true,
"/root/mathLabV2/${students[6]}": true,
},
);
// Add to database.
// This is a batched (atomic) update that either succeeds or all fails
// both database additions as one transaction.
// .update will add records when they don't exist (or replace them if they do).
await _referenceRoot.update(_atomicUpdate);
根据下面的评论进行更新 要检查“mathLabV2”下是否存在学生 ID,我将在 Flutter/Dart 中使用以下代码......您需要修改其语法以满足您的需求,但概念是相同的:
_referenceRoot.child("root").child("mathLabV2").child(<your_student_id>).once()
.then((DataSnapshot snapshot) {
if (snapshot.value != null) {
<Do your update processing on the 'students' node using <your_student_id>>;
}
},
),
关于javascript - 如何一次为每个用户添加一个子级到另一个子级 Firebase 下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59932256/