Elonas Marcauskas
Elonas Marcauskas

Reputation: 361

Extra field from Firebase Document snapshot

For some odd reason I am receiving document snapshots from Firebase that include extra fields defining other fields. The following is a sample JSON from a DocumentSnapshot:

{
"_fieldsProto": {
    "general_knowledge": {
        "mapValue": {
            "fields": {
                "“What do ya mean?”": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                },
                "question": {
                    "stringValue": "In the TV show friends, what is Joey Tribbiani’s famous line?",
                    "valueType": "stringValue"
                },
                "“How you doin’?”": {
                    "booleanValue": true,
                    "valueType": "booleanValue"
                },
                "“Who’s home?”": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                }
            }
        },
        "valueType": "mapValue"
    },
    "science": {
        "mapValue": {
            "fields": {
                "question": {
                    "stringValue": "Who is considered the “father” of organic chemistry?",
                    "valueType": "stringValue"
                },
                "Charles Darwin": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                },
                "Gregor Mendel": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                },
                "Friedrich Wohler": {
                    "booleanValue": true,
                    "valueType": "booleanValue"
                }
            }
        },
        "valueType": "mapValue"
    },
    "music": {
        "mapValue": {
            "fields": {
                "question": {
                    "stringValue": "Who released the 2018 album “Graffiti U”?",
                    "valueType": "stringValue"
                },
                "Adam Levine": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                },
                "Keith Urban": {
                    "booleanValue": true,
                    "valueType": "booleanValue"
                },
                "John Legend": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                }
            }
        },
        "valueType": "mapValue"
    },
    "history": {
        "mapValue": {
            "fields": {
                "question": {
                    "stringValue": "The Incan Empire is located in which modern-day country?",
                    "valueType": "stringValue"
                },
                "Peru": {
                    "booleanValue": true,
                    "valueType": "booleanValue"
                },
                "The United States of America": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                },
                "New Zealand": {
                    "booleanValue": false,
                    "valueType": "booleanValue"
                }
            }
        },
        "valueType": "mapValue"
    }
},
"_ref": {
    "_firestore": {
        "projectId": "trivia-that-pays"
    },
    "_path": {
        "segments": ["daily_play_questions", "000086"]
    },
    "_converter": {}
},
"_serializer": {
    "allowUndefined": false
},
"_readTime": {
    "_seconds": 1625961162,
    "_nanoseconds": 980162000
},
    "_createTime": {
        "_seconds": 1611029311,
        "_nanoseconds": 23101000
    },
    "_updateTime": {
        "_seconds": 1611200786,
        "_nanoseconds": 3550000
    }
}

Field such as _fieldProto, booleanValue, valueType, mapValue, fields, _ref, _serializer, _readTime, _createTime, _updateTime never existed before. The following is the JSON I used to get:

{
"general_knowledge": {
    "“What do ya mean?”": "false",
    "question": "In the TV show friends, what is Joey Tribbiani’s famous line?",
    "“How you doin’?”": "true",
    "“Who’s home?”": "false"
},
"science": {
    "question": "Who is considered the “father” of organic chemistry?",
    "Charles Darwin": "false",
    "Gregor Mendel": "false",
    "Friedrich Wohler": "true"
},
"music": {
    "question": "Who released the 2018 album “Graffiti U”?",
    "Adam Levine": "false",
    "Keith Urban": "true",
    "John Legend": "false"
},
"history": {
    "question": "The Incan Empire is located in which modern-day country?",
    "Peru": "true",
    "The United States of America": "false",
    "New Zealand": "false"
}
}

I am receiving this data by using Firebase Functions. Here is the code I am using to get the data.

const db = admin.firestore();

exports.func = functions.https.onRequest((req, res) => {

    const dailyQuestionBankRef = db
              .collection("daily_play_questions"); // set collection reference

    const selectedQuestionSet = await dailyQuestionBankRef
              .doc(randomNumber)
              .get();

    res.status(200).send(selectedQuestionSet.data());
});

randomNumber is just a number randomly generated to specify the document. There is more code in between, but is most likely tied to how the randomNumber is generated. So the problem is with the data that I get. Is this something have to do with the method I am using to get the data or am I setting something up incorrectly? The way I used to get the data, is the way my database is setup in Firebase. I don't understand what the extra fields have to do with anything.

Upvotes: 1

Views: 145

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50900

I just noticed your function is not async but you are trying to use await. I wonder how your function ran at first place. But please try the following:

export const getData = functions.https.onRequest((req, res) => {
    const dailyQuestionBankRef = admin.firestore().collection("tests"); // set collection reference

    return dailyQuestionBankRef.doc("Test1").get().then((selectedQuestionSet) => {
        return res.status(200).send(selectedQuestionSet.data());
    });
})

Alternatively you can just make the function async like this:

export const getData = functions.https.onRequest(async (req, res) => {
  const dailyQuestionBankRef = admin.firestore().collection("tests"); // set collection reference

  const selectedQuestionSet = await dailyQuestionBankRef.doc("Test1").get()
  return res.status(200).send(selectedQuestionSet.data());
})

Upvotes: 1

Related Questions