是我在firebase数据库中的数据的层次结构。我想将此数据发送到Google工作表,但无法发送。
Google工作表中指出的错误是“ TypeError:无法从未定义中读取属性“ rytf1JLXetTIINjHNku0yAvs7su2”。 详细信息”
下面是我试图检索数据的代码。请让我知道错误。谢谢!
function writeSheets() {
var ss = SpreadsheetApp.openById("google-sheet-id");
var firebaseUrl = "database-url";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData('users/admin');
Logger.log(data);
var num = 2;
range = ss.getRange("A" + num + ":E" + num + "");
for (var i in data) {
var values = [
[
[
[data][data][i] // Error occurs here
].email, [
[data][data][i] // Error occurs here
].userName, [
[data][data] // Error occurs here
[i]
].Password, [
[data][data][i] // Error occurs here
].loginState, [
[data][data][i] // Error occurs here
].uid
]
];
range.setValues(values);
num += 1;
range = ss.getRange("A" + num + ":E" + num + "");
}
}
数据样本值
{
rytf1JLXetTIINjHNku0yAvs7su2:
{
loginDetail:
{
uid:rytf1JLXetTIINjHNku0yAvs7su2,
password:123456,
loginState:true,
userName:John,
email:a@a.com
}
}
}
答案 0 :(得分:1)
从您的问题和评论中,我认为您想这样做。
"email", "userName", "password", "loginState", "uid"
的值放在电子表格的每一行。对象data
的结构如下。您要从对象loginDetail
中检索每个值。
{
"rytf1JLXetTIINjHNku0yAvs7su2": {
"loginDetail": {
"uid": "rytf1JLXetTIINjHNku0yAvs7su2",
"password": 123456,
"loginState": true,
"userName": "John",
"email": "a@a.com"
}
}
};
如果我的理解是正确的,那么该修改如何?
loginDetail
,并检索loginDetail
的对象。loginDetail
中,依次检索"email", "userName", "password", "loginState", "uid"
的每个值。"email", "userName", "password", "loginState", "uid"
的值是从电子表格第一页的第二行输入的。function writeSheets() {
var ss = SpreadsheetApp.openById("google-sheet-id");
var firebaseUrl = "database-url";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData('users/admin');
Logger.log(data);
// I modified below script.
var keys = ["email", "userName", "password", "loginState", "uid"];
var res = [];
for (var i in data) {
for (var j in data[i]) {
if (j == "loginDetail") {
res.push(keys.map(function(e) {return data[i][j][e]}));
}
}
}
var num = 2;
ss.getRange("A" + num + ":E" + (res.length + num - 1)).setValues(res);
}
data
的结构发生了变化,则需要遍历该对象。那时,请告诉我。