I am attempting to cycle through one of my bookmarks folders and log the title and url of the bookmarks from the array. Whenever I am trying to log the array for testing, an object doesn't appear at all, nothing is null or undefined. The first 2 logs show the arrays with their designated nodeTree location. What am I missing in the function to return the title and url of each bookmark to the console?
chrome.bookmarks.getTree(function(bmTree){
bmTree.forEach(function(node){
console.log(node);
// * Variables are hardcoded for development
// * reaches Resources folder in Coding Folder
// TODO: declare variable = to userInput from bookmark tree
let list = node.children[0].children[2].children[1];
console.log(list);
// Retrieve bookmarks title and url
for (let i = 0; i < list.length; i++){
const bkmk = [
list.children[i].title,
list.children[i].url
];
console.log(bkmk);
};
});
})
};
The answer was a simple tweak, instead of using list.length, I needed to use list.children.length. Code samples below:
Original:
chrome.bookmarks.getTree(function(bmTree){
bmTree.forEach(function(node){
console.log(node);
// * Variables are hardcoded for development
// * reaches Resources folder in Coding Folder
// TODO: declare variable = to userInput from bookmark tree
let list = node.children[0].children[2].children[1];
console.log(list);
// Retrieve bookmarks title and url
for (let i = 0; i < list.length; i++){
const bkmk = [
list.children[i].title,
list.children[i].url
];
console.log(bkmk);
};
});
})
Fixed:
const getBookmarks = () =>{
chrome.bookmarks.getTree(function(bmTree){
bmTree.forEach(function(node){
console.log(node);
// * Variables are hardcoded for development
// * reaches Resources folder in Coding Folder
// TODO: declare variable = to userInput from bookmark tree
let list = node.children[0].children[2].children[1];
console.log(list);
// Retrieve bookmarks title and url
for (let i = 0; i < list.children.length; i++){
var bkmk = [list.children[i].title, list.children[i].url];
console.log(bkmk);
};
});
})