0

So I have the following code:

function get_menu_location($id)
{
    const menu = wp.api.collections.Posts.extend({
       url: wpApiSettings.root + 'menus/v1/menus/' + $id,
    });

    const Menu = new menu();

    Location.fetch().then((posts) => {
        console.log(posts.items);
        return posts.items;
    });
}
document.getElementById("menu_id").innerHTML = get_menu($id);

When I call: get_menu(4) alone, I get the following console output:

enter image description here

This is the error that I'm getting:

enter image description here

How would I be able to pull in that data to my <div id="menu_4"></div> and output the results inside HTML?

  • [`textContent`](https://developer.mozilla.org/en/docs/Web/API/Node/textContent)+[`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)? – Yevhen Horbunkov Jan 14 '21 at 13:34
  • @YevgenGorbunkov, do you have an example or tutorial that I could use by any chance? –  Jan 14 '21 at 13:35
  • Move the assignment to `innerHTML` *inside* the `then` callback, as that callback will only run when the response is available, unlike what you are doing now. And there format the output with `JSON.stringify(post.items)` – trincot Jan 14 '21 at 13:36

0 Answers0