使用 Firestore REST API 和本地模拟器套件创建新文档,返回错误 404:路径参数问题

2024-04-12

作为一名初学者,我刚刚熟悉 Firebase/Firestore,并且正在尝试为朋友为其项目编写的一组可调用函数创建集成测试。我正在编写一个测试来使用 Firebase 本地模拟器套件进行自动化测试。

现在,我正在尝试使用 Axios 编写一个 POSt 请求,在收到生成授权用户的 Id 令牌后,该请求将在本地模拟器套件的给定集合中创建一个文档。

  • 项目 ID 名为 okane-crud-dev。我创建了一个集合 称为测试。

  • 我使用给定的电子邮件和密码创建了一个经过身份验证的用户,并从初始发布请求生成了唯一的 ID 令牌:

         interface createPostRequest {
         url: string;
         data: Object;
         config: Object;
     };
    
     //create an instance of a user 
     const createUserInstance : createPostRequest = {
         url: 'http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=hi',
         data: {
             'email': '[email protected] /cdn-cgi/l/email-protection',
             'password': 'mypassword',
             'returnSecureToken': true
         },
         config: {
             'headers':
                 {'Content-Type': 'application/json'}
         },
     };
     const createUserResponse = await axios.post(createUserInstance.url, createUserInstance.data, createUserInstance.config);
    
     const userIdToken = createUserResponse.data.idToken;
     const userLocalId = createUserResponse.data.localId;
    

到目前为止,我还没有遇到任何问题。

至于第二个创建文档的POST请求,这是我的代码。我用这篇文章作为参考:使用返回 HTTP 400 的 Firestore REST API 创建新集合和文档 https://stackoverflow.com/questions/56397947/creating-new-collection-and-document-with-firestore-rest-api-returning-http-400

const createDocumentInstance : createPostRequest = {
        url: "https://firestore.googleapis.com/v1beta1/projects/'localhost:8080/okane-crud-dev'/databases/(default)/documents/test",
        data: {
            "fields": {
                "localId": userLocalId,
                'budget': '2000',
            }
        },
        //directly pasted IdToken as using the variable resulted in problem with ' ' error
        config: {
            'headers': 
                {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${userIdToken}`,
                }
    }};
    console.log(createDocumentInstance);

    const createDocument = await axios.post(createDocumentInstance.url, createDocumentInstance.data, createDocumentInstance.config); 
    const docReference = createDocument.data;
    console.log(docReference);

当我尝试运行此程序时,返回以下错误:

    Request failed with status code 404

  at createError (../../node_modules/axios/lib/core/createError.js:16:15)
  at settle (../../node_modules/axios/lib/core/settle.js:17:12)
  at IncomingMessage.handleStreamEnd (../../node_modules/axios/lib/adapters/http.js:293:11)

我是初学者,刚刚开始学习如何编码,所以如果这是一个简单的答案,请耐心等待,因为我仍在弄清楚如何调试。

我知道 404 错误意味着定位资源时出现问题 -> 对标头进行一些调整后,我认为问题一定出在我的 URL 中。我尝试查找其他使用本地模拟器套件和 POST 请求的帖子,以确定我编写路径的方式是否有问题。

"https://firestore.googleapis.com/v1beta1/projects/'localhost:8080/okane-crud-dev'/databases/(default)/documents/test"

我一直在仔细查看 Firebase 文档来创建文档;https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/createDocument#path-parameters https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/createDocument#path-parameters

借用另一篇文章,我尝试了包含模拟器套件端口的不同变体:localhost:8080 和项目 ID“okane-crud-dev”。但好像还没想明白。我确保项目 ID 已连接到我的本地模拟器套件。有没有人有什么建议?


如果您将 Firestore 模拟器与 REST API 结合使用,则应更改基本 URLhttps://firestore.googleapis.com/v1到你的本地主机http://localhost:8080/v1然后继续处理 Firestore 数据库的路径。

http://localhost:8080/v1/projects/okrane-crud-dev/databases/(default)/documents/test
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Firestore REST API 和本地模拟器套件创建新文档,返回错误 404:路径参数问题 的相关文章

随机推荐