javascript - 如何一次将每个用户的孩子添加到另一个孩子火力基地下?
问题描述
主题标题可能令人困惑。students
我有孩子下的用户。我也有mathlab
孩子。我想要做的是让每个学生的身份证都在matlab
孩子下面,并在孩子下面添加一些特定的students
孩子。
所以如果学生在 mathlab 创建 "class:"math" , ps:"A" 在students/id/
.
这是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 并推入一个数组。但是我如何一次创建孩子呢?
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”值。我如何立即为所有“数学孩子”签名?
解决方案
我不得不说我仍然不太确定你想要实现什么。但是,我认为您正在尝试执行一次性事务或原子更新,以在一次调用中添加/更新您的数据库。然后,如果出现问题,整个事务就会失败,否则一切都会成功......以维护数据库的完整性。
所以,假设我是对的,这里有一个 node.js 云函数(由其他函数调用,因此不会自行导出),我用它来从我的 Firebase RTDB 中删除记录或节点。关键是它是一个“原子更新”,所以它要么全部工作要么全部失败……保持数据完整性。在我使用“null”删除记录的情况下,您可以使用“math”、“A”等向适当的子项添加或更新记录。您只需为这些孩子设置正确的参考。然后单个语句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”的值。我自己还没有尝试过。
进一步更新: 我决定尝试一下。以下代码生成了这个 db 结构。您需要选择您喜欢的以及您希望如何处理更新和删除。
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);
在下面的评论之后更新 要检查学生 id 是否存在于“mathLabV2”下,我将在 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>>;
}
},
),
推荐阅读
- python - Python Elasticsearch 客户端 - 更新每个请求的标头
- javascript - 如何从html中的另一个页面更改xml文件
- javascript - Angular:错误 NG8001:“组件”不是已知元素
- image-segmentation - 如何在 ITK-snap 中测量分段 ROI 的最大值?
- angular - 带有 mat-icon 的 mat-select-trigger 未在 div 元素中显示
- amazon-web-services - Firebase 云消息传递是否需要 AWS SNS?
- javascript - 具有标准数组的 js 上的 Javascript 动态过滤器
- spring - Tomcat战争文件部署在服务器
- python - ValueError:形状 (None, 3) 和 (None, 1) 不兼容
- prometheus - 如何从普罗米修斯绘制每天的平均值