我正在开发一个社交媒体应用程序。当用户共享某些内容时,我会使用 for 循环处理手机上的所有内容。
这是我的Java代码:
DatabaseReference reffollowers =FirebaseDatabase.getInstance().getReference().child("users").child(auth.getUid()).child("followers");
reffollowers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
List<String> followerslist = new ArrayList<>();
for(DataSnapshot ds:dataSnapshot.getChildren()){
String a = ds.getKey();
followerslist.add(a);
}
DatabaseReference refpost = FirebaseDatabase.getInstance().getReference().child("posts");
for(int i=0;i<followerslist.size();i++){
refpost.child(followerslist.get(i)).child("unique_post_key").setValue("");
}
progressDialog.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
但这需要太多时间。我希望Firebase函数来处理它,这是我的javascript代码,但它不起作用。我不懂javaScript。
这是我的javascript代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sharePost = functions.https.onCall((data, context) => {
const postkey = data.text;
const uid = context.auth.uid;
var followers_list [];
var ref = admin.database().ref("users").child(uid).child("followers");
ref.once("value", function(snapshot) {
snapshot.forEach(function(_child){
followers_list.push(_child.key);
});
});
for(count2 = 0; count2 < takipci_listesi.lengt; count++) {
admin.database().ref('posts/' + followers_list[count2]).set({
postkey: ""
});
}
});
你们能帮我吗?
我的火力数据库也是这样的
+root
+posts
+user_uid
+unique_post_key:""
当我想获取帖子时,我使用此代码传递帖子密钥
dataSnapshot.getkey();
我用这段代码解决了我的问题:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sharePost = functions.https.onCall((data, context) => {
const postkey = data.text;
const uid = data.uid;
var list= [];
var db = admin.database();
var ref = db.ref("users").child(uid).child("followers");
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
list.push(childKey);
console.log(list[list.length-1]+" eklendi");
console.log("list size : "+list.length);
});
var refsave = db.ref("posts");
for (var i = 0; i < list.length; i++) {
refsave.child(list[i]).set({
postkey:"a"
});
console.log(list[i]+" yazıldı");
}
});
});