Chall
December 8, 2023, 9:01pm
1
Does this not work anymore? Using Node.js
<dmx-value id="var1" dmx-bind:value="123"></dmx-value>
<script>
function myFunction() {
var myVar = dmx.parse('var1.value');
console.log(myVar);
}
</script>
I get undefined.
Teodor
December 8, 2023, 9:02pm
2
Your post is quite unclear.
Please explain your issue/question more detailed so we can answer it.
Chall
December 8, 2023, 9:08pm
3
Sorry, the code didn’t show up. Fixed it.
Essentially I’m trying to get dynamic data in my javascript so I can add dynamic schema data to my pages. I’m starting basic, but can’t seem to even get the basics to work.
Teodor
December 8, 2023, 9:09pm
4
When do you call myFunction()
? In the code you pasted, it is just defined and i don’t see it getting called anywhere.
Chall
December 8, 2023, 9:10pm
5
Yes, when I call it in the console it returns undefined.
Teodor
December 8, 2023, 9:11pm
6
Ok, but where is var1 being placed? Is it in a content page?
Chall
December 8, 2023, 9:13pm
7
This is just a simplified example based on this:
But yes, It is within the body of the content page.
Teodor
December 8, 2023, 9:14pm
8
Well, when it’s on a content page then you need to prefix var1 with content.
in your custom js code:
<dmx-value id="var1" dmx-bind:value="123"></dmx-value>
<script>
function myFunction() {
var myVar = dmx.parse('content.var1.value');
console.log(myVar);
}
</script>
Chall
December 8, 2023, 9:15pm
9
Ok great. I didn’t realize that. What I’m really trying to do is this:
<script>
function setSchema() {
var schemaData = {
"@context": "http://schema.org",
"@type": "Physician",
"name": dmx.parse('serverconnect1.data.query.name'),
"jobTitle": "Aviation Medical Examiner",
"url": "https://www.ameforesight.com/ame-profile/" + dmx.parse('serverconnect1.data.query.slug'),
"address": dmx.parse('serverconnect1.data.query.address'),
"telephone": dmx.parse('serverconnect1.data.query.phone_number'),
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": dmx.parse('serverconnect4.data.query[0].average_rating'),
"reviewCount": dmx.parse('serverconnect4.data.query[0].number_of_ratings')
}
};
var script = document.createElement('script');
script.type = 'application/ld+json';
script.textContent = JSON.stringify(schemaData);
document.head.appendChild(script);
}
</script>
The function would then be called when serverconnect4 is complete.
Teodor
December 8, 2023, 9:18pm
10
All the elements which you want to access in your custom js code and are located inside a content page must be prefixed with content.
Chall
December 8, 2023, 9:18pm
11
Ok, I think you have given me all I need. Really appreciate the prompt response!
1 Like