How to reference a child of a child

时间:2018-07-25 04:23:55

标签: firebase firebase-realtime-database actions-on-google dialogflow

I am currently using Firebase and Dialogflow. I wish to take in 2 parameters,courses and facilities Location, and access the information of the child of a child, however I am not sure how to do so. I will attach an image of the database for reference.

Firebase Database:

Firebase Database reference

    else if (action === 'course_day') {                                    //For courses 
    const product = request.body.queryResult.parameters.courses.trim();
    const product = request.body.queryResult.parameters.facilitiesLocation.trim();
    const ref = db.ref(`courseday/${product.toLowerCase()}`);

    ref.once('value').then((snapshot) => {
        const result = snapshot.val();
        if (result === null) {
            response.json({
                fulfillmentText: `We do not have such a course.Simply say, for example, 'What day is Chinese Calligraphy on?'!`
            });
            return;
        }
        response.json({
            fulfillmentText: `${product} lessons are on ${result.day}`,
            source: action
        });

    }).catch((err) => {
        response.json({
            fulfillmentText: `I don't know what is it`
        });

    });

1 个答案:

答案 0 :(得分:1)

On calling the 'once' function in the 'datasnapshot' you will receive the whole underlying data as a JSON. So all you have to do is move into this JSON and get the required value.

In this case, let's say your ref is pointing to 'test' branch of your db then when you call ref.once you will receive everything under that branch.

Hope this helps