Unchecked runtime.lastError: Cannot create item with duplicate id - My id is coming from Firebase

Unchecked runtime.lastError: Cannot create item with duplicate id - My id is coming from Firebase

尝试在我的上下文菜单中显示我的 firebase 实时数据库中存在的播放列表列表,以便我可以将数据保存到该播放列表。我可以为我的页面上下文显示它们,但不能为图像或引号上下文显示它们。

我在控制台中收到每个玩家 ID 的错误消息:

未选中 runtime.lastError:无法创建具有重复 ID 的项目 -MMqelZvPmCUbuMCxQ42

如何在不出现此错误的情况下 运行 在所有上下文中显示我的播放列表。

这是代码:

firebase.auth().onAuthStateChanged(function (user) {
  if (user) {
    var uid = auth.currentUser.uid;
    console.log(uid);
    //contextmenu for page CHANGE REPO TO MOST RECENT PLAYLIST -------------
    chrome.contextMenus.create({
      title: "Repo",
      contexts: ["page"],
      onclick: MyGenericClick,
    });
    chrome.contextMenus.create({
      title: "Repo",
      contexts: ["image"],
      onclick: MyImageClick,
    });
    chrome.contextMenus.create({
      title: "Repo",
      contexts: ["selection"],
      onclick: MyQuoteClick,
    });
    //playlist contextmenu from database
    database
      .ref(uid)
      .once("value")
      .then((snapshot) => {
        console.log(snapshot.val());
        snapshot.forEach(function (childSnapshot) {
          // display existing playlists
          chrome.contextMenus.create({
            id: childSnapshot.val().idKey,
            title: childSnapshot.val().name,
            contexts: ["page"],
            onclick: MyPagePlaylistClick,
          });
          chrome.contextMenus.create({
            id: childSnapshot.val().idKey,
            title: childSnapshot.val().name,
            contexts: ["image"],
            onclick: MyImagePlaylistClick,
          });
          chrome.contextMenus.create({
            id: childSnapshot.val().idKey,
            title: childSnapshot.val().name,
            contexts: ["selection"],
            onclick: MySelectionPlaylistClick,
          });
        });
      });
  } else {
    console.log("No user is signed in.");
  }
});

不知道您正在使用的确切 API,我的直觉是您需要使每个 id 在您的数据库调用中都是唯一的;目前您对每个条目使用相同的 childSnapshot.val().idKey。试试像

          chrome.contextMenus.create({
            id: childSnapshot.val().idKey + '-page',
            title: childSnapshot.val().name,
            contexts: ["page"],
            onclick: MyPagePlaylistClick,
          });
          chrome.contextMenus.create({
            id: childSnapshot.val().idKey + '-image',
            title: childSnapshot.val().name,
            contexts: ["image"],
            onclick: MyImagePlaylistClick,
          });
          chrome.contextMenus.create({
            id: childSnapshot.val().idKey + '-selection',
            title: childSnapshot.val().name,
            contexts: ["selection"],
            onclick: MySelectionPlaylistClick,
          });

使每个 id 条目更加独特。