[英]How to query data from Firebase Realtime DB if the key has multiple words?
I have something like this in my DB where some keys like "Class Units" consists of two or more words. 我的数据库中有类似的内容,其中某些键(如“类单位”)由两个或多个单词组成。
What I'm trying to do is to read the data and print it to my console, but im having trouble reading the values from these multiple-word keys. 我想要做的是读取数据并将其打印到控制台,但是我无法从这些多字键读取值。
database.ref('7290').once('value').then(function(snapshot) {
var course = snapshot.val();
console.log(course.Career);
});
I was able to print the value for Career
because it's only one word. 我能够打印出Career
的值,因为它只有一个字。
Is there anything I can do to get the value for keys that are two words since I can't call course.Class Units
? 由于无法调用course.Class Units
我有什么办法可以获取两个单词的键的值?
Can anyone help please? 有人可以帮忙吗?
Thank you! 谢谢!
So javascript provides you a couple ways to get to object properties: dot notation ( course.Career
) or brackets ( course["Career"]
). 因此,javascript为您提供了两种获取对象属性的方法:点表示法( course.Career
)或方括号( course["Career"]
)。
Using the second, we can have keys that are non-standard, as you are getting. 使用第二个键,您将得到非标准的键。 Here's a sample (note I'm just creating an object and using that, rather than fetching from FireBase, but the principle is the same): 这是一个示例(请注意,我只是创建一个对象并使用该对象,而不是从FireBase中获取,但是原理是相同的):
let course = { "id": 7290, "Career": "UGRD", "Catalog": 402, "Class Units": 3, "Component Code": "LEC", "Component Descr": "Lecture", "Course ID": "031564", "Equivalent Courses": "SOCI 402", "Long Title": "CONTEMPORARY SOCIOLOGY", "Pre Requisite Description": "Prereq: SoCI 302", "Subject": "SOCI" }; /** * The first option below uses dot notation, as you have * used in your current code. **/ console.log("Dot notation, Career: "+course.Career); /** * The second one uses brackets to get to the object key. * doing this, we can have names that are more non-standard. **/ console.info("Brackets, Class Units: "+course['Class Units'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.