dmAPI.dynamicPageApi().isDynamicPage() returns error that it's not a function

I copied and pasted the sample code directly from https://developer.duda.co/docs/collections-js-api-copy#usage

Console error: TypeError: dmAPI.dynamicPageApi(...).isDynamicPage(...).then is not a function

The code in my widget

// Verifies page is a dynamic page - this throws an error
dmAPI.dynamicPageApi().isDynamicPage().then(api => {
console.log(api);
})

// Gets dynamic page data - this works
dmAPI.dynamicPageApi().pageData().then(api => {
console.log(api);
})

What am I missing here?

Best Answers

  • Ryan_Burke
    Ryan_Burke Member Posts: 37 Duda Staff
    Answer ✓

    Thank you for catching this! @Russ_Jeffery updated the example - it should work now. Sorry about that :) Thanks Russ!

  • Russ_Jeffery
    Russ_Jeffery Member Posts: 10 App Team
    Answer ✓

    As an example of what Arnoldo asked — since it's async, you can do two things:


    Wrap in an async function and run:

    async function run() {
    const dynPage = dmAPI.dynamicPageApi(); // check if it's a dynamic page if(dynPage.isDynamicPage()) { //get the data const collectionRowData = await dynPage.pageData();
    console.log(collectionRowData) }
    }
    run();

    Or you can use it with a then/catch:

    // get dynamic page API
    const dynPage = dmAPI.dynamicPageApi(); // check if it's a dynamic page
    if(dynPage.isDynamicPage()) {
    //get the data and resolve with then()
    dynPage.pageData().then((data) => {
    // page data available here
    console.log(data)
    }).catch((e) => console.error(e));
    }

Answers

  • Buildertrend
    Buildertrend Member Posts: 2

    Thank you. Do you have suggestions to use that inside of widget?

    After adding that directly to my widget, I get the following error

    Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules

  • Arnoldo_L
    Arnoldo_L Member Posts: 17 Duda Staff

    Hey John,

    Can you please confirm that you have wrapped your function with the async keyword?