许多Firebase函数中只有一种被调用

时间:2019-11-06 23:45:19

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

我有两个要在有Http请求时执行的Firebase函数,一个函数(createEmailList)将数据保存在Firebase数据库中,另一个(zohoCrmHook)要保存在其中名为Zoho的第三方CRM。

将功能部署到Firebase时,功能日志显示两者均已正确部署。但是,当从前端发出Http请求时,日志显示仅一个功能(createEmailList)正在执行。

enter image description here

如日志所示,第一个功能createEmailList正在执行,并且数据毫无问题地显示在Firebase数据库中。但是,第二个功能zohoCrmHook甚至没有执行。

index.js

const functions = require('firebase-functions');
const admin = require("firebase-admin")
const serviceAccount = require("./service_account.json");
const createEmailList = require('./createEmailList')

// zoho
const zohoCrmHook = require('./zohoCrmHook')

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://landing-page.firebaseio.com"
})

exports.zohoCrmHook = functions.https.onRequest(zohoCrmHook)
exports.createEmailList = functions.https.onRequest(createEmailList)

createEmailList.js

const admin = require('firebase-admin')
const cors = require('cors')({ origin: true })

module.exports = (req, res) => {
    cors(req, res, () => {
        if (!req.body.email) {
            return res.status(422).send({ error: 'Bad Input'})
        }

        const email = String(req.body.email)
        const firstName = String(req.body.firstName)
        const lastName = String(req.body.lastName)

        const data = {
            email,
            firstName,
            lastName
        }

        const db = admin.firestore()
        const docRef = db.collection('users')
            .doc(email)
            .set(data, { merge: false })
            .catch(err => res.status(422).send({ error: err }))

        res.status(204).end();    
    })
}

zohoCrmHook.js

const axios = require('axios');
const functions = require('firebase-functions');

// zoho
const clientId = functions.config().zoho.client_id;
const clientSecret = functions.config().zoho.client_secret;
const refreshToken = functions.config().zoho.refresh_token;
const baseURL = 'https://accounts.zoho.com';

module.exports = async (req, res) => {
    const newLead = {
        'data': [
        {
            'Email': req.body.email,
            'Last_Name': req.body.lastName,
            'First_Name': req.body.firstName,
        }
        ],
        'trigger': [
            'approval',
            'workflow',
            'blueprint'
        ]
    };

    const { data } = await getAccessToken();
    const accessToken = data.access_token;

    const leads = await getLeads(accessToken);
    const result = checkLeads(leads.data.data, newLead.data[0].Email);

    if (result.length < 1) {
        try {
            return res.json(await createLead(accessToken, newLead));
        }
        catch (e) {
            console.log(e);
        }
    }
    else res.json({ message: 'Lead already in CRM' })
}

function getAccessToken () {
    const url = `https://accounts.zoho.com/oauth/v2/token?refresh_token=${refreshToken}&client_id=${clientId}&client_secret=${clientSecret}&grant_type=refresh_token`;

    return new Promise((resolve, reject) => {
      axios.post(url)
        .then((response) => {
          return resolve(response);
        })
        .catch(e => console.log(e))
    });
}

function getLeads(token) {
    const url = 'https://www.zohoapis.com/crm/v2/Leads';

    return new Promise((resolve, reject) => {
      axios.get(url, {
        headers: {
          'Authorization': `Zoho-oauthtoken ${token}`
        }
      })
        .then((response) => {
          return resolve(response);
        })
        .catch(e => console.log(e))
    })
}

function createLead(token, lead) {
    const url = 'https://www.zohoapis.com/crm/v2/Leads';

    return new Promise((resolve, reject) => {
        const data = JSON.stringify(lead);
        axios.post(url, data, {
        headers: {
            'Authorization': `Zoho-oauthtoken ${token}`
        }
        })
        .then((response) => {
            console.log(response.data)
            return resolve(response);
        })
        .catch(e => reject(e))
    })
}

function checkLeads(leads, currentLead) {
    return leads.filter(lead => lead.Email === currentLead)
}

1 个答案:

答案 0 :(得分:1)

由于要导出两个FROM php:7.1-fpm-alpine LABEL maintainer="Vincent Composieux <vincent.composieux@gmail.com>" RUN set -ex \ && apk --no-cache add \ postgresql-dev RUN apk add --update \ php7-common \ php7-fpm \ php7-apcu \ php7-ctype \ php7-curl \ php7-dom \ php7-gd \ php7-iconv \ php7-json \ php7-intl \ php7-mcrypt \ php7-mbstring \ php7-opcache \ php7-openssl \ php7-pdo \ php7-pdo_sqlite \ php7-xml \ php7-xmlwriter \ php7-zlib \ php7-phar \ php7-tokenizer \ php7-session \ php7-simplexml \ php7-xdebug \ php7-bcmath\ php7-intl \ php-fileinfo \ php7-redis \ php-gd \ make \ curl \ nano \ g++ \ icu-dev \ libxslt \ libxslt-dev RUN docker-php-ext-configure pdo_mysql && docker-php-ext-install mysqli pdo_mysql && docker-php-ext-enable pdo_mysql RUN docker-php-ext-install bcmath RUN docker-php-ext-configure intl && docker-php-ext-install intl RUN docker-php-ext-install xsl RUN rm -rf /var/cache/apk/* && rm -rf /tmp/* && \ curl --insecure https://getcomposer.org/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer ADD php.ini /etc/php7/php-fpm.d/ ADD php.ini /etc/php7/cli/conf.d/ ADD php.ini /etc/php7/cli/conf.d/ ADD xdebug.ini /etc/php7/conf.d/ ADD www.conf /etc/php7/php-fpm.d/ RUN sed -i "s|memory_limit = 128M|memory_limit = 512M |g" /etc/php7/php.ini RUN sed -i "s|upload_max_file_size = 2M|upload_max_file_size = 1024M |g" /etc/php7/php.ini RUN sed -i "s|post_max_size = 8M|post_max_size = 1024M |g" /etc/php7/php.ini RUN sed -i "s|max_execution_time = 30|max_execution_time = 600 |g" /etc/php7/php.ini CMD ["php-fpm7", "-F"] WORKDIR /var/www/vhosts/project EXPOSE 9000 声明,因此最终将得到两个Cloud Functions,每个Cloud Function具有各自的URL /端点。因此,如果这是您的需要,则需要配置两个调用这些功能的Web挂钩。


但是,从阅读您的问题开始,听起来更像是您想要一个具有两个功能的Cloud Function,在这种情况下,您只应具有一个php -m声明,然后该声明将调用两个常规JavaScript函数(例如)。

所以更像是:

pdo_mysql

由于您无法传递请求和响应,因此您需要在此处确定传递给两个函数调用的内容。

或者,您可以从此处调用这两个Cloud Functions,但这通常只会增加您的成本而几乎没有收益。