javascript - 未经检查的 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.");
}
});
解决方案
不知道您正在使用的确切 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
条目更加独特。
推荐阅读
- serverless-framework - serverless-dynamodb-local web shell 错误
- asp.net - Microsoft ReportViewer:将多个图像分组为精灵
- java - 如何将复杂的 Java 对象实例传递给 Python 脚本
- rx-java2 - RetryWhen() 上的类型错误有延迟
- php - Symfony 4 - 使用学说的一种形式的两个实体,一个实体必须在另一个之前插入
- javascript - javascript路径+“。” + 键不起作用
- javascript - JSRender 循环遍历字段
- python - 将 numpy 数组附加到多索引数据帧
- javascript - 特定于材质 UI 的文本字段验证意外行为
- javascript - 通过javascript设置Facebook邮箱的内容(可编辑的div)