首页 > 解决方案 > 未经检查的 runtime.lastError:无法创建具有重复 ID 的项目 - 我的 ID 来自 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.");
  }
});

标签: javascriptfirebasecontextmenu

解决方案


不知道您正在使用的确切 API,我的直觉是您需要id在数据库调用中使每个 API 都是唯一的;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条目更加独特。


推荐阅读