简体   繁体   English

有什么方法可以从角度组件使用 onCall Firebase 函数?

[英]There is any way to use onCall Firebase functions from an angular component?

I understand that firebase cloud functions can be called using the functions.https.onRequest or the functions.https.onCall, but I can't find an example about how use the "onCall" from an angular module, the examples that I found are only with the http request angular module.我知道可以使用functions.https.onRequest 或functions.https.onCall 调用firebase 云函数,但是我找不到关于如何使用角度模块中的“onCall”的示例,我找到的示例是仅适用于 http 请求角度模块。

example:例子:

import { Component } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import * as firebase from 'firebase/app';

import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/toPromise';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {


  constructor(private db: AngularFireDatabase, private http: Http) { }

  postRequest() {
    const url = 'https://your-firebase-project.cloudfunctions.net/secureEndpoint';

    firebase.auth().currentUser.getIdToken()
            .then(authToken => {
              const headers = new Headers({'Authorization': 'Bearer ' + authToken });

              const myUID    = { uid: 'current-user-uid' };    // success 200 response
              const notMyUID = { uid: 'some-other-user-uid' }; // error 403 response

              return this.http.post(url, myUID, { headers: headers }).toPromise()
            })
            .then(res => console.log(res))
    }

}

there is any way to use the JS standard call for this on an angular module?有什么方法可以在角度模块上使用 JS 标准调用?

<script src="https://www.gstatic.com/firebasejs/5.4.1/firebase-functions.js"></script>
firebase.functions().httpsCallable('FUNCTION');

I thank you guys for the guidance, you where right, I just use the import in this way: 我感谢你们的指导,在正确的地方,我只是以这种方式使用导入:

import * as firebase from 'firebase';

and then just call the functions. 然后只调用函数。

This will import all the library in the component: 这将导入组件中的所有库:

import * as firebase from 'firebase';

To import only functions , use: 要仅导入功能 ,请使用:

import * as firebase from 'firebase/app';
import 'firebase/functions';

Please check the following link https://angularfirebase.com/snippets/trigger-http-cloud-functions-from-an-angular-component/ 请检查以下链接https://angularfirebase.com/snippets/trigger-http-cloud-functions-from-an-angular-component/

if you have created the oncall request you call the cloud function this way .. 如果创建了oncall请求,则以这种方式调用cloud函数。

Get Cors Package from here 从这里获取Cors套餐

https://www.npmjs.com/package/cors

Your Cloud Function : 您的云功能:

import * as functions from 'firebase-functions';
const email = require("emailjs/email");
const cors = require('cors')({origin: true});

exports.sendMailFunction = functions.https.onRequest((request, response) => {
    cors(request, response, () => {  
        // *Your code*

        });

  });

Your Cloud url can be : https://us-central1-<projectid>.cloudfunctions.net/sendMailFunction 您的Cloud网址可以是: https://us-central1-<projectid>.cloudfunctions.net/sendMailFunction

Your Component Function that will called on click or any event you like 您的组件函数,将在单击或您喜欢的任何事件时调用

sendEmail() {

    let url = `https://your-cloud-function-url/function`
    let params: URLSearchParams = new URLSearchParams();
    let headers = new Headers({'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });

    params.set('to', 'user@example.com');
    params.set('from', 'you@yoursupercoolapp.com');
    params.set('subject', 'test-email');
    params.set('content', 'Hello World');

    return this.http.post(url, params, headers)
                    .toPromise()
                    .then( res => {
                      console.log(res)
                    })
                    .catch(err => {
                      console.log(err)
                    })

  }

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

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