【问题标题】:How to make a post request with a custom token document ref with firebase firestore Rest API如何使用 firebase firestore Rest API 使用自定义令牌文档 ref 发出发布请求
【发布时间】:2021-08-21 10:21:07
【问题描述】:

下面是我的代码,我正在尝试使用 Firebase Firestore Rest API 发送发布请求。它可以工作,但它会生成一个随机 ref id 令牌,将其链接到我发送的帖子数据(字段),这使得关联起来很困难。

我希望能够将我的帖子数据与自定义 id 令牌相关联。

final String customIdToken9 = 'customIdToken9'
String url = "https://firestore.googleapis.com/v1/projects/myAppId/databases/(default)/documents/customers/$customIdToken9" 
const _body = {
"fields": {
    "androidNotificationToken": {
        "nullValue": null
    },
    "fullname": {
        "stringValue": "Custom"
    },
    "uid": {
        "stringValue": "$customIdToken9"
    },
    "admin": {
        "stringValue": ""
    },
    "email": {
        "stringValue": "customer1@gmail.com"
    },
    "photo": {
        "stringValue": ""
    },
    "coverPhoto": {
        "stringValue": ""
    },
    "bio": {
        "stringValue": ""
    },
    "role": {
        "stringValue": "user"
    },
    "mobile": {
        "stringValue": "092222"
    }
}
 }
   http.post(url, body = _body);

当我在上面运行这段代码时,它会返回

{
"error": {
    "code": 400,
    "message": "Document parent name \"projects/MyAppId/databases/(default)/documents/customers\" lacks \"/\" at index 59.",
    "status": "INVALID_ARGUMENT"
}

}

如果我删除 customTokenId9,它可以工作,但它会生成一个我不想要的随机令牌 ID

String url = "https://firestore.googleapis.com/v1/projects/myAppId/databases/(default)/documents/customers/" 

http.post(url, body = _body);

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore http-post


    【解决方案1】:

    如果要指定自己的文档名称,则需要在 URL 请求中添加一个名为 documentId 的查询参数。以下是它在您的代码上的外观:

    final String customIdToken9 = 'customIdToken9'
    String url = "https://firestore.googleapis.com/v1/projects/myAppId/databases/(default)/documents/customers?documentId=$customIdToken9" 
    const _body = {
    "fields": {
        "androidNotificationToken": {
            "nullValue": null
        },
        "fullname": {
            "stringValue": "Custom"
        },
        "uid": {
            "stringValue": "$customIdToken9"
        },
        ...
      }
    }
    

    用于创建文档的客户端分配的文档 ID。它是可选的。如果不指定,服务会自动分配一个随机ID。

    完整的描述在这个link 中陈述,你会在查询参数部分看到它。您还可以尝试测试 API 并将其展开以查看其 curl 等效项。

    此外,您可以查看此documentation 以了解查询参数的工作原理。它适用于 Firebase 提供的任何 REST API。

    【讨论】:

    • 很好,你在哪里找到这个原因我已经检查了所有我找不到解决方案的文档
    • 嗨@OkaforZuruoke,我已经更新了我的答案以进一步解释有关查询参数的详细信息。我还提供了其他文档链接,因此您也可以查看。
    • 嘿@OkaforZuruoke!让我们知道这是否回答了您的问题。如果是这样,那么请通过单击答案旁边的复选标记接受它来关闭问题。这样其他人就知道你已经(充分地)得到了帮助。另见What should I do when someone answers my question?
    【解决方案2】:

    为什么你的变量是final String customIdToken 而你使用的是$customIdToken9 它应该是$customIdToken

    【讨论】:

    • 这是我的堆栈溢出详细实现中的一个错字。没关系。主要是如何使用自定义 Id 令牌文档向 Firebase Firestore 发出发布请求
    猜你喜欢
    • 2020-06-17
    • 2019-02-19
    • 2018-08-22
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    相关资源
    最近更新 更多