I have dynamic pages which conditionally set structured data on their page.
var structuredData = {
"@context": "http://schema.org",
"@graph": []
}
var faqs = {
"@type": "FAQPage",
"name": thing.title,
"mainEntity": []
}
if (thing.faqAnswer1 != "" && thing.faqAnswer1 != null) {
faqs.mainEntity.push(
{
"@type": "Question",
"name": `Question 1 ${thing.title}?`,
"acceptedAnswer": {
"@type": "Answer",
"text": thing.faqAnswer1
}
})
} // Repeat this 3 more times
if (faqs.mainEntity.length > 0) {
structuredData['@graph'].push(faqs)
return wixSeo.setStructuredData([
structuredData
]).then(() => {
console.log("structured data set");
console.log(wixSeo.structuredData)
} ).catch( () => {
console.log("failed setting structured data");
} )
}
For one page, everything runs perfectly, and my structure data is present and validated by tools, including Schema.org, Google Rich Results Test, and Classy Schema.
However, for other pages, nothing happens. None of the tools can detect the structured data, well, because it’s not there. I’ve printed structuredData before assignment and manually validated it, and the setStructuredData() returns “structured data set”. Looking at the source of my page, I can see that empty structured data has been set:
<script type="application/ld+json">{}</script>
Stranger is that console.log(wixSeo.structuredData) prints the full structure, validating that it was actually set but not printed.
Can someone please tell me what is going on???