node.js - Firebase 函数 shuffle firestore 数组 onUpdate
问题描述
我想得到Question Array
其中充满了多张地图。这是我的 Firebase 结构:Firebase 结构
然后我想用shuffle
函数对其进行洗牌,然后在我的 Firestore 中更新它。
exports.shuffleSet = functions.firestore
.document('duell/{duell_id}')
.onCreate((snap, context) => {
const data = snap.data();
const questionsArr = data.set.question;
console.log(questionsArr);
const shuffle = (array) => {
var currentIndex = array.length, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
console.log("Geshuffled: " + array);
return array;
}
return questionsArr.update(shuffle(questionsArr));
});
我总是在我的日志中得到TypeError: questionsArr.update is not a function
和。Function execution took 22 ms, finished with status: 'error'
我做错了什么?
解决方案
更新方法是DocumentReference
类的方法。所以你必须snap.ref
按如下方式调用它:
exports.shuffleSet = functions.firestore
.document('duell/{duell_id}')
.onCreate((snap, context) => {
const data = snap.data();
const questionsArr = data.set.question;
console.log(questionsArr);
const shuffle = (array) => {
var currentIndex = array.length, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
console.log("Geshuffled: " + array);
return array;
}
return snap.ref.update({ shuffle: shuffle(questionsArr) });
});
推荐阅读
- python - Python-Docx:如何识别段落是否斜体
- javascript - Javascript 不接受 JSON 数据作为变量
- python - 如何从这些数据创建点图(不是散点图)?
- javascript - Javascript 在我的 htm 引导程序中不起作用
- task - 在不阻塞正在进行的代码的情况下在 TCL 中获得某种时间表的方法
- javascript - FasiAPI comm : 在 js 中获取 python 回调并从 FastAPI 访问 javascript
- arrays - 计算二维数组中不同元素的出现次数
- python - 如何使用 Cloud Pubsub 发送电子邮件通知
- javascript - 如何获取在javascript中具有共同价值的对象键?
- android - ReactNative:如何在 pubnub 通知 android 中自定义声音?