简体   繁体   English

通过 Cloud Firestore 添加阵列 REST API

[英]Add array via Cloud Firestore REST API

I am trying to send data to FireStore form Airtable by using Firestore REST API, and I am stack with sending an array.我正在尝试使用 Firestore REST API 将数据从 Airtable 发送到 FireStore,我正在发送一个数组。 I get the array as a string in Firestore.我在 Firestore 中将数组作为字符串获取。 But the outcome should be with Housekeeping and Transportation tags as separate items of the array.但结果应该是 Housekeeping 和 Transportation 标签作为数组的单独项目。 屏幕

const createTagsArr = (arr) => { let r = []; arr.forEach(obj => r.push(obj.name)); return r} 

for (let i = 0; i < query.records.length; i++) {
   
    if (query.records[i].getCellValueAsString("approved")) {
        let obj = {
            "fullDescription": query.records[i].getCellValueAsString("fullDescription"),
            "tags": createTagsArr(query.records[i].getCellValue("tags").filter(obj => obj.name)),
            "id": query.records[i].getCellValueAsString("initialForm"),             
        }
        arr.push(obj)
    }
}

const pushData = async () => {
    for (let i = 0; i < arr.length; i++) {
       
        let data = {
            fullDescription: { stringValue: arr[i].fullDescription },
            tags: { arrayValue: {values: [{stringValue: JSON.stringify(arr[i].tags)}]} },

        let response = await fetch(`https://firestore.googleapis.com/v1/projects/{project-name}/databases/(default)/documents/{coolection}/${arr[i].id}?updateMask.fieldPaths=fullDescription&updateMask.fieldPaths=tags`, {
            method: "PATCH",
            headers: {
                "Content-Type": "application/json",
                "Authorization": "Bearer " + token
            },
            body: JSON.stringify({ "fields": data })
        })
        console.log(response)
    }

}


const init = async () => {
    console.log(providersArr)
    await pushData()
}

init()

If I remove JSON.stringify() from this line: tags: { arrayValue: {values: [{stringValue: JSON.stringify(providersArr[i].tags)}]} } I am getting bad request error.如果我从这一行中删除 JSON.stringify() : tags: { arrayValue: {values: [{stringValue: JSON.stringify(providersArr[i].tags)}]} } 我收到错误的请求错误。 I would appreciate any help.我将不胜感激任何帮助。 Thank you!谢谢!

Each value of an array must have a key of stringValue .数组的每个值都必须有一个stringValue键。 To be able to separate the values of arr[i].tags , you should first iterate your arr[i].tags , construct the object and push it in an array.为了能够分离arr[i].tags的值,您应该首先迭代arr[i].tags ,构造 object 并将其放入数组中。 See sample code below:请参阅下面的示例代码:

const pushData = async () => {
  for (let i = 0; i < arr.length; i++) {
    // Initiate an empty array.
    let tags = [];

    // Iterate the `arr[i].tags` to create an array of objects.
    for (const tag of arr[i].tags) {
      // Construct the object to be pushed in the initialized array.
      tags.push({ stringValue: tag });
    }

    let data = {
        fullDescription: { stringValue: arr[i].fullDescription },
        // Use the created array here.
        tags: { arrayValue: {values: tags} },
    }

    let response = await fetch(`https://firestore.googleapis.com/v1/projects/{project-name}/databases/(default)/documents/{collection}/${arr[i].id}?updateMask.fieldPaths=fullDescription&updateMask.fieldPaths=tags`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + token
        },
        body: JSON.stringify({ "fields": data })
    })
    console.log(response)
  }
}

The code above will result to:上面的代码将导致:

在此处输入图像描述


For more information, you may check out these documentation:有关更多信息,您可以查看这些文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cloud Firestore 使用 REST API 更新嵌套字段 - Cloud Firestore update nested field using the REST API 如何在不使用 firestore 删除 rest 的情况下在阵列上添加 object? - How to add an object on an array wthout to delete the rest with firestore? FireStore REST API 调用 - FireStore REST API call Firestore REST API,POST 请求成功,文档未出现在模拟器 UI 中,云触发器未触发 - Firestore REST API, POST request succeeds, document does not appear in emulator UI, and cloud triggers do not fire 如何使用从 FIrestore 读取的数据创建云 Function REST API 端点 - How to create Cloud Function REST API endpoints with the read data from FIrestore 降低 Cloud Firestore API 延迟 - Lowering Cloud Firestore API Latency 使用 Cloud Firestore REST API 如何使用 http package 和 http.post() 方法发送数据 - Using Cloud Firestore REST API how to send data using http package with http.post() method 自定义 Rest API 和 Firestore 数据库 - Custom Rest API And Firestore Database Firestore REST API | 如何使用 firestore rest api 和 postman - Firestore REST API | How to use firestore rest api with postman 如何通过 cli / rest api / 云功能运行 Google Cloud Build 触发器? - How to run a Google Cloud Build trigger via cli / rest api / cloud functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM