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

时间:2018-09-05 20:54:06

标签: javascript angular firebase google-cloud-functions

我了解可以使用功能https:// onRequest或功能https.onCall来调用firebase云功能,但是我找不到关于如何从角度模块中使用“ onCall”的示例,我发现只有http请求角度模块。

示例:

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))
    }

}

有什么方法可以在Angular模块上使用JS标准调用吗?

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

3 个答案:

答案 0 :(得分:1)

我感谢你们的指导,在正确的地方,我只是以这种方式使用导入:

import * as firebase from 'firebase';

然后调用函数。

答案 1 :(得分:0)

这将导入所有组件中的库:

import * as firebase from 'firebase';

要仅导入功能,请使用:

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

答案 2 :(得分:-2)

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

如果您创建了通话请求,则可以通过这种方式调用云函数..

从此处获取Cors软件包

<xs:element name="totalInactiveUsersOfMonth" type="job:nonNegativeLongType" >

您的云功能:

char tDirectory[] = "C:\\Windows\\System32\\notepad.exe"; 

    if (!CreateProcess(tDirectory, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) {

        printf("Error creating process, with error code: %d\n", GetLastError());
        return 1;

    }

    printf("Process created...");

    return 0;

}

您的Cloud网址可以是:https://www.npmjs.com/package/cors

您的组件功能,将在点击或您喜欢的任何事件时调用

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*

        });

  });