gpt4 book ai didi

firebase - Google Apps Scripts - 如何从 Firestore 获取文档的关键值?

转载 作者:行者123 更新时间:2023-12-04 10:46:14 25 4
gpt4 key购买 nike

我正在尝试获取 Firestore 数据库中文档的键值,但没有获取到。

此值如下:
enter image description here

这是我的代码:

function objectsToArray(objects) {

var outputArray = [];
for (var i in objects){
outputArray.push([

objects[i].fields.id, objects[i].fields.data, objects[i].fields.acao,
objects[i].fields.categoria, objects[i].fields.movimentos, objects[i].fields.descricao

]);

}
return outputArray;
}

我的 JSON 输出 Logger.log:
 [20-01-11 19:42:06:370 CET] [{"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc","fields": 
{"categoria":"SUPERMERCADO","data":"2019-07- 31T00:00:00.000Z","descricao":"","acao":"Despesa","movimentos":23.82,"id":107},
"createTime":"2019-12-31T14:35:47.959299Z","updateTime":"2019-12- 31T14:35:47.959299Z"},

有什么建议?

谢谢

最佳答案

原始数据的结构似乎是这样的:

 [
{
"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc",
"fields": {
"categoria":"SUPERMERCADO",
"data":"2019-07- 31T00:00:00.000Z",
"descricao":"",
"acao":"Despesa",
"movimentos":23.82,
"id":107
},
"createTime":"2019-12-31T14:35:47.959299Z",
"updateTime":"2019-12- 31T14:35:47.959299Z"
},

您想要的值: 0MwgqEm9abho3bpB5yCc
哪个在带有 name 的元素中属性键。
function objectsToArray(objects) {
var L,nameValue,finalValue;

objects = [
{
"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc",
"fields": {
"categoria":"SUPERMERCADO",
"data":"2019-07- 31T00:00:00.000Z",
"descricao":"",
"acao":"Despesa",
"movimentos":23.82,
"id":107
},
"createTime":"2019-12-31T14:35:47.959299Z",
"updateTime":"2019-12- 31T14:35:47.959299Z"
},
]

var outputArray = [];

L = objects.length;

for (var i=0;i<L;i++){

nameValue = objects[i].name;
Logger.log('nameValue: ' + nameValue)

finalValue = nameValue.slice(nameValue.lastIndexOf("/")+1);

Logger.log('finalValue: ' + finalValue)

outputArray.push(finalValue);

}

Logger.log('outputArray: ' + JSON.stringify(outputArray))
return outputArray;
}

关于firebase - Google Apps Scripts - 如何从 Firestore 获取文档的关键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59690487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com