0

Guys I am using expressjs, ejs to develop an online shopping website.

Scenario In order to display products {name, description, imageUrl} I am iterating with the help of ejs control-flow on the webpage.

I am sending an array that contains all the details about each product in JSON format. (from server) on the client-side when I try to access this array of jsons I get the following error.

(index):266 Uncaught SyntaxError: Invalid or unexpected token

When I opened the sources tab in google chrome it, showed me this

enter image description here

At line number 266. I believe that it is taking this as a string by wrapping the array in double quotes.?

Could anyone tell me what's going wrong with this code?

And by the way, is how I trying to print the array on chrome console

  <script>
    const data = JSON.parse("<%- products %>");
    console.log(data);
  </script>

where products are an array of jsons

Backend home route which is sending the JSON array is as follows

router.get("/", async (req, res) => {
  let catalogInformation = {};
  //getAllProducts returns array of json from the database
  const products = await getAllProducts();
  catalogInformation.products = products;
  userData = {
    email: null,
    username: null,
  };
  catalogInformation.userData = userData;
  if (req.session.userInformation != null || req.session.userInformation != undefined) {
    catalogInformation.userData = req.session.userInformation;
  } else {
    catalogInformation.userData = {
      email: null,
      username: null,
    };
  }
  // res.render("catalog", userData);
  res.render("catalog", catalogInformation);
  return;
});

getAllProducts() method

const getAllProducts = async () => {
  const Product = mongoose.model("product", productSchema);
  const allProducts = await Product.find();
  return allProducts;
};
2
  • Can you please provide the code on the backend? Commented Oct 27, 2020 at 14:05
  • It looks like the array is generated in the getAllProducts() function. Are you able to provide the code for that function? Commented Oct 27, 2020 at 14:36

1 Answer 1

1

You don't need to use JSON.parse. Just output the JSON directly:

var data = <%- products %>;

I wouldn't suggest making an AJAX call because you'd be handling 2 requests instead of 1.

Sign up to request clarification or add additional context in comments.

6 Comments

But why is the output not wrapped in [ ]? though I am sending an array.
I don't know, I'd have to see the backend code to be able to help.
Hey, I just added the backed route. If this is not enough. I will share my git repo link here.
Yeah, I added the products method. Please take a look.
Nothing about the code looks like it wouldn't/shouldn't output the brackets. Did the backquote work? Are the brackets still not outputting with the updated code?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.