跳到主要内容

节点元数据与标签

节点元数据与标签可用于为节点添加自定义信息与标签,以便更好地组织与管理。

概述

在 RainMaker 中,节点元数据与标签可以:

  • 为节点添加标签以便分类。
  • 更新并管理节点元数据。
  • 按需移除标签与元数据。

添加标签

使用 attachTags 方法为节点添加标签。

try {
const tags = ["living-room", "smart-lighting", "priority"];
const response = await node.attachTags(tags);
console.log("Tags attached successfully:", response);
} catch (error) {
console.error("Error attaching tags:", error);
}

移除标签

使用 removeTags 方法移除节点标签。

try {
const response = await node.removeTags();
console.log("Tags removed successfully:", response);
} catch (error) {
console.error("Error removing tags:", error);
}

更新元数据

使用 updateMetadata 方法更新与节点关联的元数据。

try {
const metadata = {
location: "Living Room",
installationDate: "2024-01-15",
notes: "Main lighting control"
};

const response = await node.updateMetadata(metadata);
console.log("Metadata updated successfully:", response);
} catch (error) {
console.error("Error updating metadata:", error);
}

删除元数据

使用 deleteMetadata 方法删除与节点关联的元数据。

try {
const response = await node.deleteMetadata();
console.log("Metadata deleted successfully:", response);
} catch (error) {
console.error("Error deleting metadata:", error);
}

示例:管理标签与元数据

try {
const response = await userInstance.getUserNodes();
const node = response.nodes[0];

// 添加标签
await node.attachTags(["smart-home", "lighting"]);

// 更新元数据
await node.updateMetadata({
room: "Bedroom",
deviceType: "Smart Light"
});

// 访问标签与元数据
console.log("Node tags:", node.tags);
console.log("Node metadata:", node.metadata);
} catch (error) {
console.error("Error managing tags and metadata:", error);
}

On this page