简体   繁体   English

Firebase + Cloud功能动态onCall

[英]Firebase + Cloud functions dynamic onCall

I'm working on an angular/firebase web app and I started creating cloud functions onCall. 我正在使用angular / firebase网络应用程序,并且开始在onCall上创建云函数。

My app is based on a chat room: 我的应用基于聊天室:

messages
    |__1
       |__ 0
           |__ author: "foo"
           |__ message: "hello world"
           |__ playerExcluded: "foo2"
       |__ 1
           |__ author: "foo2"
           |__ message: "hello world"
           |__ playerExcluded: "foo"

I want to list all messages where currentUser != playerExluded (the user can see all messages except messages where he is excluded), that's why I started looking for cloud function. 我想列出currentUser!= playerExluded的所有消息(用户可以看到除他以外的消息以外的所有消息),这就是为什么我开始寻找云功能的原因。 I'm just trying some things first before doing that, but it's not working as I want. 在执行此操作之前,我只是先尝试​​一些操作,但并没有达到我想要的效果。

Here is what I wrote: 这是我写的:

Cloud function: 云功能:

const getMessages = functions.https.onCall((data, context) => {
    console.log('data: ', data);
    return new Promise((resolve, reject) => {
        admin.database().ref('/messages').child(data).on('value', msg => {
            resolve(msg.val());
        });
    })
});

module.exports = { getMessages };

(I used Promise because it was returning nothing without it. I used .on and not .once because I need to get messages dynamically). (我使用Promise是因为没有它,它什么也不会返回。我使用.on而不是.once是因为我需要动态获取消息)。

Angular part: 角部分:

Service: 服务:

messages: Message[] = [];
messagesSubject = new Subject<Message[]>();

getMessages() {
    firebase.functions().httpsCallable("getMessages")("1").then(result => {
        console.log("result: ", result);
        this.messages = [result.data[0]]; // let's try with first message
        console.log(this.messages);
        this.emitMessages();
    }).catch(error => {
        console.log("error:", error);
    });
}
emitMessages() {
    this.messagesSubject.next(this.messages);
}

Component: 零件:

messages;
messagesSubscription: Subscription;

constructor(private messageService: MessageService) { }

ngOnInit() {
    this.messageService.getMessages();
    this.messagesSubscription = this.messageService.messagesSubject.subscribe((result) => {
        console.log(result);
        this.messages = result;

    });
}

All will load fine. 一切都会好起来的。 But it'll not be dynamic, I'll need to refresh my page to see changes. 但这不是动态的,我需要刷新页面以查看更改。

Did I do something wrong ? 我做错什么了吗 ? Is firebase queries ".on" incompatible with cloud functions .onCall and httpsCallable (if yes, any suggestion ?) ? Firebase查询“ .on”是否与云函数.onCall和httpsCallable不兼容(如果是,则有任何建议吗?)?

You can't use a callable function to deliver ongoing results. 您不能使用可调用函数来提供持续的结果。 You must return a single set of results from onCall, then the function terminates. 您必须从onCall返回一组结果,然后函数终止。 If you require more updates, you have to call the function again, but it's better if the client can use the Firebase SDKs to query the database directly and listen to the results as they change. 如果需要更多更新,则必须再次调用该函数,但最好是客户端可以使用Firebase SDK直接查询数据库并在结果更改时侦听结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM