[英]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.