Firebase HTTP Post 请求出现 404 Not Found 错误(“无法发布”)

问题描述 投票:0回答:1

这个问题已经被困扰好几天了:我正在尝试编写一个 firebase 云函数,该函数基于 HTTP post 触发器来更新 firestore 集合中的文档。我已经部署了 firebase 函数,但是当我点击 url 并向其发布数据时,我不断收到 404 消息。我尝试使用邮递员和卷曲触发它,但我收到相同的“404 Not Found”错误。

这是错误消息:

    Error



     <pre>Cannot POST /</pre>

这是云功能:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

// upload each landbot consultation to users' firestore collection

const express = require('express');
const app = express();
app.use(express.json());

app.post('/uploadConsultation', async(req,res) => {

  try{
    const consultationData = req.body;

    // Log the contents of the JSON file landbot has sent
    console.log('JSON Package sent from Landbot:', consultationData);

    // Identify the location of the "users" firestore collection
    const firestore = admin.firestore();
    const usersCollection = firestore.collection('users');

    // Extract userid and make a variable out of userid and another out of the remaining consultation data
    const {userid, ...consultation} = consultationData;

    // Check if users collection contains a document with the specified userid
    const userDocRef = usersCollection.doc(userid);
    const userDocSnapshot = await userDocRef.get();

    if (userDocSnapshot.exists) {

      // If the user id doc exists, add the remaining consultation data as a map field
      await userDocRef.update({consultation: consultationData});
      console.log('Added consultation data to user collection document:', );

    } else {

      // If user id doc doesn't exist, show error message
      console.log('There is no document in the users collection with that userid');
      res.status(422).send('No user exists with this userid');
    }
  } catch(error) {

    //handle errors using logs and sending error responses to landbot
    console.error('Error uploading consultation data');
    res.status(500).send('Error uploading consultation data to firestore');
  }
});

// Register onRequest handler
exports.uploadConsultation = functions.https.onRequest(app);

这是curl请求:

curl -X POST "http://us-central1-xxx.cloudfunctions.net/uploadConsultation" -H "Content-Type: application/json" -d '{"email": "[email protected]","userid": "ofAkmWfs73OlRLvlsZ5m9nC1t5t1","answers": {"@me_minime": "Me","@hair_challenges": "Dry hair, Breakage","@current_state_of_hair": "Natural","@curl_type": "Type 4C","@retailer": "Uche"}}'

我已经检查了所有明显的事情,但对于下一步该做什么却碰壁了。我点击的网址是从我的终端准确复制粘贴的,因此没有问题(出于隐私原因模糊了项目 ID)。

我似乎也没有进入该函数,因为日志中没有显示任何内容(函数中的第一个语句是打印日志消息)。看起来 url 端点没有被发现,而 404 错误是由实际代码的问题引起的。

我在终端中没有看到任何错误消息,表明每次我编写“firebase deploy --only functions”命令时该函数尚未正确部署。事实上,我也已经要求 firebase 列出实时运行的功能,其中列出了 uploadConsultation 功能。

对于接下来要尝试什么有什么想法吗?是否是身份验证问题导致仅返回一般 404 错误?或者是其他东西?这是我第一次使用 Firebase,所以不知道下一步该做什么,或者是否错过了某种设置步骤。

提前致谢

node.js firebase express google-cloud-functions
1个回答
0
投票

看起来您使用一个快速应用程序部署了函数,该应用程序捕获 URL 路径“/uploadConsultation/uploadConsultation”。这是因为您将函数命名为“uploadConsultation”,而 Express 应用程序需要“uploadConsultation”的嵌套路径。

如果您想要在 CURL 请求中提供的 URL 路径,您的 Express 应用程序应该查找路由“/”

app.post('/', async(req,res) => { ... })
© www.soinside.com 2019 - 2024. All rights reserved.