Solved (sort of) - TypeError in simple GetPost()

The problem appears to be when the page is executing in preview mode. When published, the console logs fire correctly and everything behaves. .

Hi all, I have a simple GetPost() which is not working:

import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';

$w.onReady(function () {

    $w('#post1').getPost().then((post) => {
            console.log("We have the post")
        })
        .catch((error) => {
            console.log('We have an error')
            console.log(error);
        });
    }
)

The page object is definitely #post1.

… each time I run this I get an error as follows:


We have an error
TypeError: Cannot read properties of undefined (reading '0')

If I modify the getPost() to this it still yields the same result

    $w('#post1').getPost().then( async (post) => {
            console.log("We have the post")
        })
        .catch((error) => {
            console.log('We have an error')
            console.log(error);
        });
    }

Any ideas what’s going on? Could the post database be corrupted?

The async in your second snippet doesn’t do anything - you don’t have any asynchronous code in the .then() function.

What is it that you are trying to do? What is #post? What post database are you referring to?

Hi Brewmaster (love the name :blush: )

The dataset is Blog/Posts, the #post1 is the post viewer object on the post page of the blog

The following code works published, but I get a TypeDef error in preview mode.

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(function () {
    wixLocation.onChange((location) => {
        initPage()
    });
    initPage()
})

async function initPage() {
    $w('#post1').getPost().then(async (res) => {
            console.log("We have the post")
            assignPostDataToUIElements(res)
        })
        .catch((error) => {
            console.log('We have an error')
            console.log(error);
        })
}

async function assignPostDataToUIElements(postRes) {
    let info = await postRes
    console.log(info)
    $w("#postTitle").text = await info.title
    $w("#postHeaderStrip").background.src = await info.coverImage
}

Aha - It could very well be that it just doesn’t work in Preview due to technical issues. I’ll check with the Blog dev team and see if they can shed some light.

The await keywords that you are using in the assignPostDataToUIElements function are not necessary. In fact, if the awaited thing is not a Promise, it will nonetheless be wrapped in a Promise, and that Promise is awaited. Theoretically the execution order is changed, but not really such that you’ll notice.

You should just remove the await from all of the statements. You can also delete the superfluous async in the getPost().then() function - it doesn’t do anything.

I’ll let you now what I hear from the devs. :beers:

Thanks Ysrael; beer is on the house should you make it to France :wink:

Beer on the house sounds great. Now if I could just get out of my house - damn Corona!

I heard back from the devs about this. It’s actually a known issue and should be fixed tomorrow. Give it a try over the next couple of days and let me know.