I’ve customized my blog post page to fetch post data from a collection in the backend using getPost(). I then use the results to “hydrate” certain elements on the page.
I also have a related posts section on this page and I’m using wixLocation.to() to handle the navigation, then wixLocation.onChange() to re-run the same code and get data for the new post. Trouble is, getPost() returns undefined
after navigating to a new article. I can’t update page elements if I can’t get to my data.
Isn’t wixLocation.onChange() supposed to remedy this kind of thing? Has anyone had similar trouble and found a solution? My best guess is that the page wixLocation.to() takes you to has no actual blog post element to speak of somehow, otherwise the getPost() function would work invariably. Cheers
Can you share some example code? As long as you can get a postId on page load and then pass that to getPost in your backend it should work. Seeing some code would be helpful to debug what the issue could be.
Hi, Anthony! Thanks for responding. Here’s the current code. I tried to keep it as simple as possible while still including everything necessary to accomplish the tasks.
let currentAuthor;
let currentPost;
$w.onReady(function () {
async function loadPostPage() {
await $w('#post1').getPost().then(post => {
currentPost = post;
initAuthor();
LoadRelatedPosts();
})
async function initAuthor() {
currentAuthor = await wixData.query('Items').eq('hed', currentPost.title).include('byline').include('tags').find();
hydrateElements(); // function that populates elements on the page using results from getPost() above. Rather long so I'm omitting it
}
wixLocation.onChange(() => {
loadPostPage();
})
loadPostPage();
})
function LoadRelatedPosts() {
return wixData.query("Blog/Posts")
.ne("_id", currentPost._id)
.hasSome("categories", currentPost.categories)
.limit(9)
.find()
.then(res => {
$w('#relatedPostsRepeater').data = res._items;
})
}
I’ve customized my blog post page to fetch post data from a collection in the backend using getPost().
WHERE IS THE MENTIONED BACKEND-CODE ???
Trouble is, getPost() returns undefined
after navigating to a new article.
It will never work on FRONT-END!!! This is why you get problems.
Oh, there is no backend code. I should have said *from the backend-- my mistake.
And I hear you, but why wouldn’t a function built specifically for the Blog Post page not work on the Blog Post page? Can you suggest any alternative approaches?
As i can see, you are attempting to get your posts-data on FRONTEND, but —> getPost() normaly will work on BACKEND ONLY .
https://www.wix.com/velo/reference/wix-blog-backend/posts/getpost
You will have to reconstruct your code…
-generating an JSW-BACKEND-FILE
-generating your getPost()-function on the BACKEND
-running the generated BACKEND-FUNCTION from front-end sending the needed data to backend (POST-ID)
-getting back as result your found post to frontend again
-using the result-data on frontend
Oh, the fact that this was a backend function totally went over my head somehow.
I was able to follow the steps you listed and pass the new post ID to my backend function using my repeater’s itemData.id. Works like a charm.
Thanks for patiently explaining. You are a ninja(not that it was ever in question
) And thank you again, Anthony.