我有一个来自 firebase 的实时数据库,其中包含 9386 个数据集(但将来可能会发生变化)
这就是为什么我想知道最后一个事件的“关键”是什么。
这就是我目前试图让它知道的方式:(使用 npm 模块 qwest)
let order = "\u0022\u0024key\u0022";
let dbUrl = "https://example.firebaseio.com/users.json?limitToLast=1&orderBy=";
let end;
qwest.get(dbUrl + order)
.then(function(xhr, response) {
console.log(response);
end = response[9386]["username"];
console.log(end);
});
第一个问题:为什么我必须转义引号和美元符号?
第二个问题:如何获取我在 json 中检查的最后一项的“密钥”(limitToLast=1)。
json 响应如下所示:
{"9386":
{
"fromListA":"1",
"fromListB":"0",
"id":"9939",
"lastChecked":"2019-05-09 03:18:05",
"userid":"123456789",
"username":"username"
}
}
我想在变量中获得“9386”。
由于您选择使用"
来限制dbUrl
字符串,因此不能"
在不转义的情况下在该字符串的值内使用。定义字符串的一种更简单的方法是使用'
来分隔它:
let dbUrl = 'https://example.firebaseio.com/users.json?limitToLast=1&orderBy="$key"';
要获取对象的键,请使用以下内容:
Object.keys(response)[0]
var response = {"9386":
{
"fromListA":"1",
"fromListB":"0",
"id":"9939",
"lastChecked":"2019-05-09 03:18:05",
"userid":"123456789",
"username":"username"
}
}
console.log(Object.keys(response)[0]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句