如何从[PROJECT B]云函数(触发器)调用[PROJECT A]云函数?

时间:2021-04-15 19:09:14

标签: node.js firebase axios google-cloud-functions

期望的行为:更新 [PROJECT B] 中的问题状态时,调用指向 [PROJECT A] 中的函数的 HTTPS,使用该问题文档中的数据在那里创建用户。

我尝试了什么:

[项目 A] 功能:

    exports.createUser = functions.https.onRequest((req, res) => {
    //set JSON content type and CORS headers for the response
    res.header('Content-Type', 'application/json')
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'Content-Type')
    //respond to CORS preflight requests
    if (req.method === 'OPTIONS') {
        res.status(204).send('')
        return true
    } else {
        const body = req.body
        const caller = body.authUser
        let user = body.user 
.... ALL THE STUFF

[PROJECT B] 触发器:

const functions = require('firebase-functions');
const axios = require("axios").default;
const cors = require('cors')({ origin: true });

     exports.createUserOnAccept= functions.firestore
            .document('issues/{issueId}')
            .onUpdate((change, context) => {
        
                let old= change.before.data();
                let new= change.after.data();
        
                if (old.status!== 'Accepted' && new.status=== 'Accepted') {
    
    // PREPARE DATA STUFF
    
                var config = {
                    method: 'POST',
                    url:
                        "https://*****.cloudfunctions.net/createUser",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        authUser: '******',
                        user: [PREPARED USER DATA],
                        invoiceAddress: [PREPARED INVOICE DATA],
                    },
                };
    
                try {
                    axios(config)
                        .catch(error => {
                            functions.logger
                                .log('error', error)
                        })
                        .then(response => {
    // LOG TO SEE HOW IT COMES
                            functions.logger
                                .log('response', response)
                        });

0 个答案:

没有答案
相关问题