I have code that returns four values from the TikTok API. I can console log the results, but I’m not sure how to return them, so I can display them as HTML. I have the JS written to display the result to the DOM.
var treeVidCount = 23; document.getElementById("text-node").innerHTML = There are ${treeVidCount} tree planting videos on TikTok today. `
treeVidCount is hardcoded at 23 for now, but Iwant to use real TikTok API values.
I don’t think I can access the result from outside the promise. Where do I write return, in order to access the videoCount or viewCount? Or, how can I reduce result.challengeInfo.stats.videoCount to single variable, videoCount?
.then(result => {
// render to DOM here, where the console log is, like:
document.querySelector(".someElement").innerHTML = result.valueYouWantToRender;
})
Single variable, I was thinking of something like:
let treeVidCount = result.challengeInfo.stats.videoCount
document.getElementById("text-node").innerHTML = ` There are ${treeVidCount} tree planting videos on TikTok today. `
This would give me more freedom to access the variable in the outer scope.
I tried the querySelector code. Not sure if I made a syntax error, because it returns an object error. I’ll leave the API key public for a couple of hours until I fix this. Feel free to try the codepen:
No harm. DanCouper’s answer is the cleanest and quickest, I’ll use that. I just wondered what the other options were for future reference. Defining a separate function would work too.
I left a codepen in my previous post. Still trying to get the code to work.