javascript - 是否可以在firestore中设置数组限制?
问题描述
我打算在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。
推荐阅读
- android - 在模块 classes.jar (com.stripe:stripe-3ds2-android:4.0.5) 和 classes.jar (opentok-android-sdk-2.18.0.aar) 中发现重复的类 aaaaaa
- php - 指定源语言,如果 default_locale 不是英语
- amazon-web-services - AWS Kinesis NextShardIterator 永远不会为空
- database - Neo4J cypher:推荐产品使用 NOT 子句或任何
- c# - CefSharp WPF 代理认证
- java - int[] 不能转换为 int return 语句
- wordpress - Wordpress REST API 不工作 - 本地实例,官方 docker 镜像
- javascript - 如何在缩放或调整大小时/之后移动 HighStock 克隆的工具提示
- microsoft-dynamics - Dynamics Axe EDT 过滤器
- javascript - Javascript,如何检查对象的所有属性?是否可以将其与模式进行比较?