1
votes

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:

{"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."}

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);
      });
});
1
provide the error log, so we can understand the problem better - NeatNerd
@NeatNerd: {"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."} - Dominik Grodl
where does url parameter come from? - NeatNerd
That’s just a placeholder, in the actual code a string is there, but it contains my api key, so I replaced it with “url” here. The fetch function works fine. - Dominik Grodl

1 Answers

0
votes

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.