是否可以在消防仓库中设置阵列的限制



我计划在firestore中创建一个最多只有5个元素的数组,如下所示阵列a=[1,2,3,4,5]

然后添加元素6,它将看起来像这个

阵列a=[2,3,4,5,6]

此云函数(位于:https://github.com/firebase/functions-samples/blob/master/limit-children/functions/index.js)在实时数据库中执行您想要的操作:

'use strict';
const functions = require('firebase-functions');
// Max number of lines of the chat history.
const MAX_LOG_COUNT = 5;
// Removes siblings of the node that element that triggered the function if there are more than MAX_LOG_COUNT.
// In this example we'll keep the max number of chat message history to MAX_LOG_COUNT.
exports.truncate = functions.database.ref('/chat').onWrite((change) => {
const parentRef = change.after.ref;
const snapshot = change.after
if (snapshot.numChildren() >= MAX_LOG_COUNT) {
let childCount = 0;
const updates = {};
snapshot.forEach((child) => {
if (++childCount <= snapshot.numChildren() - MAX_LOG_COUNT) {
updates[child.key] = null;
}
});
// Update the parent. This effectively removes the extra children.
return parentRef.update(updates);
}
return null;
});

我相信你可以把它改编成Firestore。

最新更新