0

I am struggling to pass parameters via fastify to my page, as its just passing object Object as a string instead of my actual object.

This might just be me being a little stupid as im not exactly sure how fastify works, nor the formatting of the HBS files, as this was a project discontinued by the previous developer and I decided to pick it up to add missing features to it!

EDIT: after trying a couple different methods to attempt to get this to work, ive found this only happens in the script tags, so is there a different way to import this information to be used in scripts?

Here are some snippets of my code that may be useful:

index.js

/** * Our home page route * * Returns src/pages/index.hbs with data built into it */ fastify.get("/", function(request, reply) { const colors = require("./src/colors.json"); let params = { seo: seo, colors: colors }; reply.view("/src/pages/index.hbs", params); }); 

index.hbs

<form class="color-search" method="post"> <label for="sim"> <select name="sim" id="sim"> </select> </label> <label for="box"> <select name="box" id="box"> </select> </label> </form> <script> let box = $('select[name=box]'); let sim = $('select[name=sim]'); sim.append('<option>Choose one</option>'); console.log("test") console.log({{colors}}) $.each({{colors}}, function(key, entry) { sim.append($('<option></option>').attr('value', entry).text(entry.name)); }); 

colors.json

{ "Sim A": { "Box A": { "name": "", "connects": ["Box B"] }, "Box B": { "name": "", "connects": ["Box A", "Box C"] } }, "Sim B": { "Box C": { "name": "", "connects": ["Box B", "Box D"] }, "Box D": { "name": "", "connects": ["Box C", "Box E"] }, "Box E": { "name": "", "connects": ["Box D"] }, } 

And lastly what is seen in the developer console

let box = $('select[name=box]'); let sim = $('select[name=sim]'); sim.append('<option>Choose one</option>'); console.log("test") console.log([object Object]) // this is whats wrong $.each([object Object], function(key, entry) { sim.append($('<option></option>').attr('value', entry).text(entry.name)); }); 

any ideas why colors is being passed as object Object instead of the actual object?

1 Answer 1

0

You need to stringify (JSON.stringify) the object before passing into view and need to use triple curly braces {{{ value }}} to evaluate.

/** * Our home page route * * Returns src/pages/index.hbs with data built into it */ fastify.get("/", function(request, reply) { const colors = require("./src/colors.json"); let params = { seo: seo, colors: JSON.stringify(colors) }; // HERE reply.view("/src/pages/index.hbs", params); }); 

index.hbs

<form class="color-search" method="post"> <label for="sim"> <select name="sim" id="sim"> </select> </label> <label for="box"> <select name="box" id="box"> </select> </label> </form> <script> let box = $('select[name=box]'); let sim = $('select[name=sim]'); sim.append('<option>Choose one</option>'); console.log("test") console.log({{{ colors }}}) // HERE $.each({{{colors}}}, function(key, entry) { sim.append($('<option></option>').attr('value', entry).text(entry.name)); }); </script> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.