尝试在 javascript 中设置映射变量值以使用云函数将文档作为映射字段进行火存。它失败并出现错误 - 错误:参数“数据”的值不是有效的 Firestore 文档。输入不是普通的 JavaScript 对象(在
field.map_field: myMap 中找到是导致问题的原因。那么如何在 firestore 文档中将 javascript map 设置为 map 字段?
admin.firestore().collection("users").doc("test").set({
name: "Los Angeles",
state: "CA",
map_field: myMap // map variable
})
完整代码在这里:我正在聚合来自多个 firestore 文档的数据并尝试插入到一个新文档中。
代码------------------------>
exports.fxname= functions.https.onRequest((request, response) => {
collectionRef = admin.firestore().collection('user/doccx/collx');
let myMap = new Map();
collectionRef.listDocuments().then(documentRefs => {
console.log('Fetching each document:console log');
return admin.firestore().getAll(...documentRefs)
.then(documentSnapshots => {
for (let documentSnapshot of documentSnapshots) {
let name = documentSnapshot.get('aa');
let totalScore= documentSnapshot.get('bb');
myMap.set(name, totalScore.toString());
}
functions.logger.log("calculation done: ");
console.log(Array.from(myMap) ); //prints values
var keys = Object.keys(myMap);
keys.forEach(key=>{
console.log(key + '|' + myMap[key]); //this is not printing any value
});
});
与问题无关,但也低于警告。它与 .then 的嵌套有关。关于重写上述代码的任何建议?
108:34 警告避免嵌套 promise promise/no-nesting
108:34 警告避免嵌套 promise promise/no-nesting
九州编程
哆啦的时光机
相关分类