Firebase Remote Config:如何通过代码创建参数组?
Firebase Remote Config 参数组创建报错解决方案
这个错误不是Admin SDK限制参数组创建,也不是SDK本身的问题,纯粹是代码逻辑遗漏:你直接访问template.parameterGroups['${country}_${poolId}'].parameters时,目标参数组对象还未初始化,所以会抛出"Cannot read properties of undefined"的错误。
解决步骤
- 先检查目标参数组是否存在于
template.parameterGroups中,如果不存在,先创建一个包含空parameters对象的参数组结构 - 再向该参数组的
parameters中添加具体参数
示例代码
const groupKey = `${country}_${poolId}`; // 先初始化目标参数组 if (!template.parameterGroups[groupKey]) { template.parameterGroups[groupKey] = { parameters: {} }; } // 现在可安全向组内添加参数 template.parameterGroups[groupKey].parameters['target_param_key'] = { defaultValue: { value: 'default_content' }, description: '自定义参数描述' // 按需添加其他参数配置项 };
补充说明
Firebase Admin SDK完全支持通过代码创建参数组,官方文档示例默认假设参数组已存在,因此未提及初始化步骤。而跳过参数组直接创建参数时,template.parameters是默认存在的空对象,所以不会触发类似错误。
内容的提问来源于stack exchange,提问作者Pasindu Dilshan
相关产品推荐
相关产品推荐

