Creating JavaScript variables from Database Results

(Gavin McGruddy) #1

Hi all,

So I seem to be going round in circles with trying to find the correct information / any forum post that touches on what I’m trying to accomplish, and have reached the point of finally reaching out, even though I’m no doubt missing something super obvious!!!

We have data in an external database that we’re pulling into our Squiz Matrix Instance with a “DB Connector” asset, and then making the necessary queries with “DB Data Source” assets.

However, I’m trying to figure out how to convert the returned results into usable JavaScript variables for use by the JavaScript files on our website, and am struggling to see how to do this!

Out of necessity of making some progress, I ended up creating “Asset Listing Pages” for each query, to insert the results into a hidden div onto the HTML page, and then using the JavaScript file to pull that data from the DOM into variables that can be usable, but I know there must be an easier way!!

I’ve tried to play around with the other assets that look like they should be able to accomplish something like this (REST Resource etc) but to be honest, I don’t do much development work in Squiz Matrix in our organization, so I’m struggling to use the CMS to accomplish this seemingly simple task!

Can anyone point me towards any tutorials, or provide a simple summary of the assets I’d need to create and establish relationships to do this?

Thanks for your time!


(Nic Hubbard) #2

You could use that same asset listing to write the javascript. Have you tried this?

(Iain Simmons) #3

Following on from Nic’s suggestion, you would put either the curly braces or square brackets in the page contents with the %asset_listing% keyword and then the default format would either be a key/value pair (if an object) or the array item (could be an object for that whole row of the DB), using the ds__ keywords for the values.

Don’t forget commas to separate array items or object properties.

The only other thing to remember is that you’ll likely need to strip the trailing comma somehow.

Most people use %asset_position^eq:1::,% at the start of the default format to only place a comma there from the second item onwards.

Hope that helps! Let us know how you go, otherwise I could share some example code.

(Nic Hubbard) #4

For this, you just create a position format with a value of -1 and it will create a position format for the last item in the list. Use this to remove the comma.

(Iain Simmons) #5

Yeah that’s another option, though it tends to be less obvious, and also slightly more annoying to maintain because then you’d have to change code in two places each time (the one with the comma and the one without).

(Gavin McGruddy) #6

face palm Can’t believe I didn’t think of this…

I’ve now simply outputted the Database Results on the page between a opening and closing .

Using yours and Ian’s replies I was able to improve my code immensely - thanks so much!

(Gavin McGruddy) #7

Thanks Iain, that’s most helpful and I was able to take on board Nic’s suggestion of simply outputting the Database results into a tag in the page, and then use your “%asset_position^eq1::,%” suggestion to correctly format the object.

Thanks so much!