Firebase Function在本地运行,但部署失败 [英] Firebase Function runs locally but deployment fails

查看:74
本文介绍了Firebase Function在本地运行,但部署失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发需要在Firebase上运行其后端代码的应用程序.我从未使用过Typescript和Functions,因此很可能我的代码中存在一个错误,阻止了该功能的部署.让我感到烦恼的是,这些功能通过firebase在本地运行没有问题,但是,当我要部署该功能时,出现错误,说明该功能未正确部署.当我使用debug运行它时,它表示该错误可能是由于用户代码中的错误所致.错误日志:

I am currently working on an app for which I need a back-end code running on Firebase. I've never worked with Typescript and Functions, so it is very likely that there is a bug in my code which prevents the function from deploying. What bugs me is that the functions runs locally with firebase serve without a problem, but then, when I want to deploy the function, I get error saying that the function did not deploy properly. When I run it with debug, it says that the error was probably due to a bug in user code. Error log:

{代码":3,消息":功能在加载用户代码时失败.这可能是由于用户代码中的错误所致.错误消息:错误:请检查功能日志以查看错误原因:cloud.google.com/functions/docs/monitoring/logging#viewing_logs.其他疑难解答文档可在cloud.google.com/functions/docs/troubleshooting#logging中找到.请访问cloud.google.com/functions/docs/troubleshooting,以获取详细的故障排除文档."}

{"code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at cloud.google.com/functions/docs/troubleshooting#logging. Please visit cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation."}

我想要的功能是从api下载数据,检查Firestore数据库中是否已经存在相同的数据,如果不存在,则写入数据库.该函数如下所示:

What I want the function to do is to download data from api, check whether the same data already exist in the Firestore database, and if not, write to the database. The function look like this:

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import fetch from "cross-fetch";

admin.initializeApp();

const db = admin.firestore();

export const scheduledFetch = functions.pubsub.schedule("*/15 * * * *").onRun( (context) => {
  fetch(url)
      .then((res) => {
        return res.json();
      }).then((data) => {
        console.log(data.articles);
        data.articles.array.forEach((article : any) => {
          const query = db
              .collection("sampleData")
              .where("title", "==", String(article.title));
          query.get().then((snapshot) => {
            if (snapshot.empty) {
              console.log("writing document");
              const data = {
                type: "article",
                author: article.author,
                title: article.title,
                description: article.description,
                url: article.url,
                urlToImage: article.urlToImage,
                publishedAt: article.publishedAt,
                content: article.content,
                source: article.source,
              };
              db.collection("sampleData")
                  .doc()
                  .set(data);
            } else {
              console.log("Already exists, not writing");
            }
          }).catch((error) => {
            console.log(error);
          });
        });
      }).catch((error) => {
        console.log(error);
      });
});

推荐答案

因此,在尝试查找问题几个小时之后,firebase函数服务似乎与交叉获取不兼容.安装并使用node-fetch之后,这些功能将正确部署.

So after few hours of trying to locate the problem it appears that the firebase functions service is somehow not compatible with cross-fetch. After installing and using node-fetch, the functions deploys properly.

这篇关于Firebase Function在本地运行,但部署失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆