【问题标题】:Cloud Functions working in emulator but not when deployedCloud Functions 在模拟器中工作,但在部署时不工作
【发布时间】:2021-11-11 05:27:04
【问题描述】:

我正在尝试在 Firebase 中创建文档后创建集合和文档。

它在模拟器中运行良好,但是当我将我的函数部署到 Firebase 项目时,它们不会创建该集合和文档。

但如果我在现有文档(快照)中创建字段,它就可以工作。

在函数中,我从 API 获取数据并将其写入新文档(目前尚未创建)。

功能:

exports.quoteEndPoint = functions.firestore.document('users/{userID}/followedStocks/{stockID}')
    .onCreate((snap, context) => {
        const stock_id = context.params.stockID;
        const user_id = context.params.userID;

        var request = require('request');
        var http = require('https');

        const options = {
            "method": "GET",
            "hostname": "alpha-vantage.p.rapidapi.com",
            "port": null,
            "path": '/query?function=GLOBAL_QUOTE&symbol='+stock_id+'&datatype=json',
            "headers": {
                "x-rapidapi-host": "%API_HOST%",
                "x-rapidapi-key": "%API_KEY%",
                "useQueryString": true

        }
    };


    const req = http.request(options, function(res){
        const chunks = [];
        res.on('data', function(chunk){
            chunks.push(chunk);
        });
        res.on('end', function(){
            const body = Buffer.concat(chunks);
            //console.log(body.toString());
            const result = JSON.parse(body.toString());
            console.log(result);

            //set values from json responso to Firebase
            return snap.ref.collection('quoteEndPoint').doc('data').set(
                {
                    'symbol': result['Global Quote']['01. symbol'],
                    'open': result['Global Quote']['02. open'],
                }, { merge: true }).then(()=>{
                    console.log('New quoteEndPoint fields for ' + stock_id + ' added to Firebase');
                })
                .catch(err => {
                    console.log(err);
                });             
        });
    })
    .on('error',(err) => {
        console.log('Error: '+err.message);
    });

    req.end();
    
    return true;

    });

我试图使函数:function() 异步,但它不起作用。

在模拟器中创建并填充正确路径的值:/users/7nDGdHmZDuoDiJkxixgz/followedStocks/AMD/quoteEndPoint/data

有人可以帮忙吗?

谢谢

【问题讨论】:

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


    【解决方案1】:

    您的问题很可能来自这样一个事实,即使用 request 的调用不会返回承诺,而在由后台事件触发的 Cloud Functions 中(例如 Firestore 的 .onCreate()),您必须返回承诺。观看official video series 了解更多详情:尤其是标题为“Learn JavaScript Promises”的 3 个视频。

    此外,request 已弃用。

    你可以使用axios,它返回一个Promise,或者node-fetch。需要chain the promises返回的异步操作,即axios和Firestore异步调用,如下代码“骨架”所示:

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');   
    const axios = require('axios');
    
    exports.quoteEndPoint = functions.firestore.document('users/{userID}/followedStocks/{stockID}')
        .onCreate((snap, context) => {
            
            const stock_id = context.params.stockID;
            const user_id = context.params.userID;
    
            return axios({
                method: 'get',
                url: 'http://....'
                // ... See the doc
            })
            .then(response => {
                // ...
    
                return snap.ref.collection('quoteEndPoint').doc('data').set(...);
            });
    
        });
    

    【讨论】:

    • 谢谢!效果很好!它给了我一个错误,但因为我还必须在函数位置运行 npm install axios。
    猜你喜欢
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    相关资源
    最近更新 更多