简体   繁体   English

如果密钥有多个单词,如何从Firebase Realtime DB查询数据?

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

相关问题 如何从Firebase实时数据库查询? - How to query from firebase realtime database? 在一个查询中从 Firebase 实时数据库的多个引用中获取数据 javascript - Get data from multiple references from Firebase realtime database in one query javascript 如何从Firebase实时数据库中检索数据? - How to retrieve data from firebase realtime database? 如何正确存储来自 Firebase 实时数据库的数据并在检索到数据后使用 React 进行渲染? - How to properly store data from Firebase Realtime Database and render it with React once it has been retrieved? Firebase 实时数据库查询显示未定义? - Firebase realtime db query showing undefined? Firebase 实时数据库,这两个查询有什么区别? - Firebase realtime DB, what is difference in these two query? 如何从 Firebase 实时数据库中获取数据,这些数据是随机密钥的子项 - 使用本机反应? - How do I fetch data from firebase realtime database which are children to a random key - using react native? {Firebase-javascript} 如何从实时数据库的两条路径中查询数据 - {Firebase-javascript} How to query data from two path of realtime database 如何从 firebase 实时数据库中获取随机推送密钥 - How to get random pushed key from firebase realtime database 如何查询firebase实时数据库获取上月数据 - How to query firebase realtime database to get last month data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM