Firebase文档快照中的额外字段



出于某种奇怪的原因,我收到了来自Firebase的文档快照,其中包括定义其他字段的额外字段。以下是来自DocumentSnapshot:的样本JSON

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

_fieldProtobooleanValuevalueTypemapValuefields_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只是一个随机生成的数字,用于指定文档。中间还有更多的代码,但很可能与randomNumber的生成方式有关。所以问题出在我得到的数据上。这是与我获取数据的方法有关,还是我设置错误?我过去获取数据的方式是在Firebase中设置数据库的方式。我不明白额外的字段与什么有关。

我刚刚注意到您的函数不是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());
})

相关内容

  • 没有找到相关文章

最新更新