简体   繁体   中英

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.

Is there anything I can do to get the value for keys that are two words since I can't call 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"] ).

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):

 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']) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM