跳到主要内容

自定义数据

在 RainMaker 中存储应用专属的用户字段、地理位置和时区。

备注

userInstance —— 在用户登录过程中获得的 ESPRMUser 实例。

本模块的功能​

在用户记录上管理自定义键值数据,可选配读写权限,并提供地理坐标和时区的辅助方法。

适用场景:用户资料中的字段不足,或需要使用位置和时区作为配网默认值。

预期结果:自定义数据保存在用户记录中;设备配网时可自动应用该时区。

常见工作流​

读写自定义数据​

const customData = await userInstance.getCustomData();

await userInstance.setCustomData({
name: {
value: "jack",
perms: [{ read: ["user", "admin"], write: ["user"] }],
},
});

await userInstance.deleteCustomData("exampleKey");
await userInstance.deleteCustomDataKey("exampleKey");
await userInstance.deleteCustomDataPermissions("exampleKey");

地理坐标​

await userInstance.setGeoCoordinates({
latitude: 28.6139,
longitude: 77.209,
});

const geo = await userInstance.getGeoCoordinates();

时区​

await userInstance.setTimeZone("Asia/Kolkata");

设置该时区后,此用户新配网的设备可继承该时区。

错误处理​

try {
await userInstance.setCustomData(customDataRequest);
} catch (error) {
console.error("Failed to save custom data:", error);
}

仅可使用支持 JSON 序列化的值,避免循环结构。

最佳实践​

  1. 在各应用版本中使用稳定的键名
  2. 尽早设置时区:如需将其作为配网默认值
  3. 不要在自定义数据中存储密钥
  4. 限定权限范围:当多个客户端读取同一用户记录时

相关资源​

On this page