javascript - 如何在承诺中检索数组值?(未捕获(承诺中)TypeError:无法读取未定义的属性“tabId”)
问题描述
我正在开发我的 chrome 扩展。我无法在承诺中检索数组值。
async function getAllTabs() {
let allTabs = [];
await chrome.windows.getAll({populate: true},function(windows){
windows.forEach(function(window){
window.tabs.forEach(function(tab){
let url = new URL(tab.url);
allTabs.push({windowId: window.id.toString(), tabId: tab.id.toString(), domain: url.hostname, title: tab.title, url: tab.url});
});
});
});
return allTabs;
}
let allTabs = getAllTabs();
allTabs.then(function(tabs) {
console.log(tabs); // no prob, an array is shown
console.log(tabs[0].tabId); // error! Uncaught (in promise) TypeError: Cannot read property 'tabId' of undefined
});
但我可以获得简单字典数组的值。
let dictArray = [];
dictArray.push({tabId: 2, windowId: 3, msg: "Hello"});
dictArray.push({tabId: 8, windowId: 6, msg: "Kitty"});
console.log(dictArray); // an array is shown
console.log(dictArray[0].tabId); // 2
解决方案
推荐阅读
- qt - QTabBar 'baseline' 样式
- c# - 有人知道如何在 Xamarin 中连接和读取 OBD-II (ELM 327) 吗?
- powershell - 在启动时制作不可见的 eShell(kiosk) 窗口
- c - 我试图遍历一个结构数组,并在我再次调用该函数(for循环)时从我离开的地方继续
- c++ - libnice - 访问套接字信息和 TURN 通道 ID
- javascript - 如何在输入中制作可拖动但不可编辑的标签
- c# - C# JSON 模型结构
- video-streaming - 如何设置我的主机名而不是 Wowza RTMP url
- spring - 如果 POST 请求中缺少请求正文,最合适的错误消息是什么?
- c++ - VTK/C++ - 将自定义 PolyData 上的标量值着色为自定义颜色