Encountering issues when trying to access a property after setting an EJS variable in

Whenever this code runs, an interesting type error occurs within the if statement. It says: Cannot read property product.thumbgallery1 of undefined.

var urlArray= [];
    var product = '<%- product %>';
     console.dir(product);


                for (var count = 1; count < 6; count++) {
                    var stringCount = count.toString();
                     if ( typeof product.data["product.thumbgallery" + stringCount].value.main.url === 'undefined' ) {
                         null
                     } else {
                            urlArray.push( product.data['product.thumbgallery' + stringCount].value.main.url )
                     }
                 }

Interestingly enough, this snippet of code actually gets the job done:

var lol = '<%- product.data["product.thumbgallery1"].value.main.url %>';
    console.log(lol);

What's happening behind the scenes here?

Answer №1

To transfer an object to EJS, you must utilize the JSON.stringify method:

 var item = <%- JSON.stringify(item) %>;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with dynamic JSON in Flutter: A concise guide

{ "status": "error", "msg": "Please ensure all fields are filled out properly", "error": { "device_token": [ "The device token field is ...

Fundamentals of Angular 2

It's not just an inconvenience, but something that truly frustrates me. Could someone please clarify the following: Why does Angular load these scripts in HTML directly from node_modules https://i.sstatic.net/D8UrG.png Why am I unable to simply imp ...

The problem arises when the KendoUI update request fails to utilize JSON format, even when explicitly setting the dataType property to

I have searched extensively through the KendoUI documentation regarding the scheduler widget, and have also sought help on the "premium" forums without success. My current objective is to send meeting data to my NodeJS API. In accordance with the document ...

What could be the reason for the history.length being 2 on the initial page?

Why is it that when I open a new browser window and load a page in it, the history.length property is always 2 in both Chrome and Firefox? You can test this phenomenon yourself by visiting the following link: http://jsbin.com/amiyaw ...

`"Attempting to access keys of a non-object value" - Dealing with JSON and React Native`

Having trouble with React Native and JSON structure? No worries, we've all been there at some point! Don't sweat it if you're still new to React and JavaScript - we're here to help! Let's take a look at the JSON structure causing ...

Troubleshooting ER_BAD_FIELD_ERROR in a node mysql stored procedure

Encountering an error message while executing the following code: Error: ER_BAD_FIELD_ERROR: Unknown column 'employee_id' in 'field list' Output: { employee_id: '6', employee_name: 'test', employee_contact: ...

What is the process of displaying text within a link?

I have a basic webpage where I want the text to display from a link. Here is my website: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Encyclopedia</title> <link href="test.css" re ...

Tips for ensuring a scrollbar remains at the bottom position

I'm facing an issue with a scroll-bar inside a div element. Initially, the position of the scroll-bar is at the top. However, whenever I add text to the div element, the scroll-bar remains in its initial position and does not automatically move to the ...

The showdown: Single Page Applications (SPA) take on Multi Page Applications (MPA)

Can you enlighten me on the differences between using React-Router and ExpressJs? I know React-Router is best for client-side rendering and Single Page Applications, while Express is more suited for Multi Page Applications. However, I'm confused about ...

Focused on individual characters in a string to implement diverse CSS styles

Is there a way I can apply different CSS classes to specific indexes within a string? For example, consider this string: "Tip. \n Please search using a third character. \n Or use a wildcard." I know how to target the first line with CSS using : ...

Tips for sending an HttpResponse and extra information through Ajax on Django

I am facing a challenge where I need to merge two AJAX requests together. The initial request retrieves HTML content: def ajax_get_html(request): if request.is_ajax() and request.method == "POST": context = { ... } ...

Tips for viewing the content of an authentication-required API in a browser

Currently, I am in the process of developing an Android project that requires accessing a specific github API. This particular API contains vital information regarding user repositories. Before diving into Android programming, I am eager to explore the c ...

Tips for adjusting column positions in a table while maintaining a fixed header and utilizing both horizontal and vertical scrolling

Is there a way to display a table that scrolls both horizontally and vertically, with the header moving horizontally but not vertically while the body moves in both directions? I have been able to shift the position of the columns, however, I am struggling ...

Creating a tree structure from a one-dimensional array with the help of dual data tables in JavaScript

I'm struggling to create a tree structure from a flat array using two Mock data tables in JSON. The table should match the unique IDs to determine the hierarchy between them. JSON with Groups DB array example: { "group": [ { "groupName" ...

Formik React struggling with error management and handling tasks accurately

I am currently using the Formik template to develop a Login Form. onSubmit={( values, { setSubmitting, setErrors /* setValues and other goodies */ } ) => { props.logMeIn(va ...

Using Javascript to create bold text within a string

I have noticed that many people are asking about this issue, but it seems like a clear and simple answer is hard to come by. Currently, I am working with Vue and trying to display text from an object in a component. My goal is to highlight the price port ...

What is the optimal method for transmitting both an image and JSON data to my express server?

Hey there! So I've set up a MongoDB database and I'm using Mongoose to work with it. Here's the model I have for my product: const productSchema = new Schema({ name: { type: String, required: true}, description: { type: String, required ...

Disable the ability to select text when double-clicking

Is there a way to prevent text selection on double click while still allowing selection on mouse drag? Whenever I try to remove selection on dblclick or mouseup, it flashes, which is not the desired outcome as shown in this jsfiddle. UPD: I am not lookin ...

Verifying if the completion of the busboy event has already happened or not

In the form I have, there is a file processing task that takes some time to complete. Currently, while node is processing the files, if it encounters the finish event, it immediately triggers it. How can I ensure that the finish event is only fired after a ...

React Mongoose data validation error

I've encountered an issue while trying to save order data in mongoose. When I attempt to add it, I receive the following error: Request: https://i.sstatic.net/HVSyg.png Response: https://i.sstatic.net/sS9fp.png Below is the source code that I am ...