You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Thingsboard:如何通过自定义动作修改当前用户属性

解决ThingsBoard点击地图标记修改当前用户属性的问题

问题分析

  1. 报错原因:你手动填写的entityId只是纯字符串ID,ThingsBoard的attributeService.saveEntityAttributes方法需要的是完整的EntityId对象(包含id和entityType字段),直接传字符串会导致服务无法识别实体类型,触发No enum constant org.thingsboard.server.common.data.EntityType.undefined错误。
  2. 当前用户EntityId获取:当前用户的实体ID可以直接从widgetContext中获取,无需手动查找——widgetContext.currentUser.id就是包含类型(USER)的合法EntityId对象。

修正后的代码

// 获取属性服务
const attributeService = widgetContext.$injector.get(widgetContext.servicesMap.get('attributeService'));

// 从地图标记的点击事件中获取建筑Nr值(根据你的地图部件实际参数调整,比如从action参数或marker实体属性取)
const targetNr = widgetContext.actionParameters?.entity?.attributes?.SERVER_SCOPE?.Nr || 0;

// 构造要更新的属性
const attributes = [{
    "key": "Nr",
    "value": targetNr
}];

// 使用当前用户的EntityId执行更新
attributeService.saveEntityAttributes(widgetContext.currentUser.id, 'SERVER_SCOPE', attributes)
    .subscribe(
        () => {
            console.log('用户属性Nr更新成功!');
        },
        error => {
            console.error('更新失败:', error);
        }
    );

关键说明

  • 当前用户EntityId:widgetContext.currentUser.id是ThingsBoard内置的当前用户实体ID对象,自带entityType: USER,完全符合attributeService的参数要求,不会再触发类型错误。
  • 获取建筑Nr值:根据你的地图部件配置,点击标记时通常可以通过widgetContext.actionParameters拿到对应标记的实体数据,比如标记绑定的建筑实体的Nr属性,需要根据实际情况调整取值路径。
  • 权限验证:既然你用「New Update Multiple Attributes」部件可以正常修改,说明用户权限没问题,只要在自定义动作中正确传递用户EntityId和目标值即可实现相同效果。

内容的提问来源于stack exchange,提问作者Hanna Kattermann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 03:18:41