How can I work with the objects returned by 'tabs.query()'?

I have three tabs open, when I print activeTab, on the console I get:

let activeTab = browser.tabs.query({currentWindow: true})

0: Object { id: 1, index: 0, windowId: 1, … }
1: Object { id: 2, index: 1, windowId: 1, … }
2: Object { id: 3, index: 2, windowId: 1, … }
length: 3

If I try to work with the object, such as getting its length or try array indexing into it, I just get undefined:

activeTab.length // undefined
activeTab[1]    //undefined

I am trying to familiarise myself with this method and its return object, such as getting the tabId property. Thanks for helping me.

browser.tabs.query() returns a Promise that resolves to an array of tab objects, but you’re trying to access the properties of the array synchronously. To work with the result of the browser.tabs.query() function, you need to use the .then() method or use an async/await approach.

browser.tabs.query({ currentWindow: true }).then(function(tabs) {

Hmm…More reasearch is needed more!
Thank you for the help and for especially clarifying this Promise thing. I will look into it and see.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.