Firebase Cloud Function 在 Prod 中不起作用。出现错误

转到 URL 时出现以下错误。


Error: could not handle the request

我似乎无法弄清楚为什么。我做错了什么?


这是我的index.js文件。


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

var request = require('request');


const admin = require('firebase-admin');

admin.initializeApp();


exports.addMessage = functions.https.onRequest(async (req, res) => {

  const itemDescription = req.query.itemDescription;

  const pageNumber = req.query.pageNumber;

  const categoryId = req.query.categoryId;

  const sortBy = req.query.sortBy;

  const narrowSearch = req.query.narrowSearch;

  cnst typeOfListing = req.query.typeOfListing;

  const sellerExclusions = req.query.sellerExclusions;

  const tagsExclusions = req.query.tagsExclusions;

  const country = req.query.country;

  const minPrice = req.query.minPrice;

  const maxPrice = req.query.maxPrice;

  const entriesPerPage = req.query.entriesPerPage;

  const buyingFormat = req.query.buyingFormat;


  let operationName = "";

  let entriesPerPage2 = "";

  let sortOrder = "";

  let currentPage = "";


  if (pageNumber !== null) {

    currentPage = pageNumber;

  } else {

    currentPage = 1;

  }


  if (typeOfListing === 'active') {

    operationName = "findItemsAdvanced";

    entriesPerPage2 = 50;

  } else {

    operationName = "findCompletedItems";

    if (buyingFormat === "Auction") {

      entriesPerPage2 = 50;

    } else {

      entriesPerPage2 = 25;

    }

  }



  let apicall = "https://URL?";




  if (typeOfListing === "active") {

    if (sortBy !== null) {

       apicall += "&sortOrder=";

       apicall += sortBy;

       sortOrder = sortBy;

    } else {

      apicall += "&sortOrder=";

      apicall += "BestMatch";

      sortOrder = "BestMatch";

    }

  } else {

    if (sortBy !== null) {

       apicall += "&sortOrder=";

       apicall += sortBy;

       sortOrder = sortBy;

    } else {

      apicall += "&sortOrder=";

      apicall += "EndTimeSoonest";

      sortOrder = "EndTimeSoonest";

    }

  }



  if (categoryId !== null) {

    apicall += "&categoryId=";

    apicall += categoryId;

  }




LEATH
浏览 89回答 1
1回答

慕桂英546537

在 Cloud Functions 中,您需要通过 Promises 管理异步方法调用。request本机支持回调接口但不返回承诺。您应该使用另一个库,例如axios,如下所示:exports.addMessage = functions.https.onRequest(async (req, res) => {    try {        // ...        let apicall = "https://URL?";        // ...        apicall += "&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value(0)=true";        const response = await axios.get(apicall);        // handle success        // ...        res.json({..});    } catch (error) {        res.status(500).send({ 'error': error });    }    });请注意,您可能需要使用“Blaze”定价计划。事实上,免费的“Spark”计划“只允许向 Google 拥有的服务发出网络请求”。请参阅https://firebase.google.com/pricing/(将鼠标悬停在“云功能”标题后面的问号上)另请注意,request已弃用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript