firebase文档快照中的额外字段

bihw5rsg  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(325)

出于某种奇怪的原因,我从firebase接收到文档快照,其中包括定义其他字段的额外字段。下面是一个示例 JSONDocumentSnapshot :

{
"_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
    }
}

字段,例如 _fieldProto , booleanValue , valueType , mapValue , fields , _ref , _serializer , _readTime , _createTime , _updateTime 以前从未存在过。以下是我用来获取的json:

{
"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"
}
}

我使用firebase函数接收此数据。下面是我用来获取数据的代码。

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只是随机生成的用于指定文档的数字。中间有更多的代码,但很可能与随机数的生成方式有关。所以问题在于我得到的数据。这是否与我获取数据所使用的方法有关,或者是我设置的不正确?我获取数据的方式,就是在firebase中设置数据库的方式。我不明白这些额外的字段和什么有什么关系。

f0brbegy

f0brbegy1#

我刚注意到你的功能不正常 async 但是你正在尝试使用 await . 我想知道你的功能最初是如何运行的。但请尝试以下方法:

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());
    });
})

或者,您也可以只创建函数 async 这样地:

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());
})

相关问题