Meteor fetching nested arrays

I am facing a challenge while trying to structure a table of orders in Meteor. Specifically, I am having difficulty displaying a nested array within the document. Below is the code snippet I am working with:

Collection data

{
    "_id" : "tDLaCMSde3QyneJqm",
    "orderNumber" : 1234,
    "createdAt" : ISODate("2016-01-11T11:14:21.986Z"),
    "productsInOrder" : [
        {
            "item" : 10300,
            "desc" : "Ergonomic Wooden Fish",
            "quantity" : "43",
            "price" : "0.92",
            "lineprice" : "39.56",
            "_id" : "BQeEwtGQEDpPxA6ZM"
        },
        {
            "item" : 98517,
            "desc" : "Refined Concrete Soap",
            "quantity" : "10",
            "price" : "2.11",
            "lineprice" : "21.10",
            "_id" : "YqBdy8aLJovuncQce"
        },
        {
            "item" : 69824,
            "desc" : "Incredible Frozen Gloves",
            "quantity" : "7",
            "price" : "3.79",
            "lineprice" : "26.53",
            "_id" : "EefPSwLHCFyJuzXcT"
        },
        {
            "item" : 14897,
            "desc" : "Intelligent Frozen Towels",
            "quantity" : "3",
            "price" : "4.15",
            "lineprice" : "12.45",
            "_id" : "BSg32fTmpqZBdM2eT"
        }
    ]
}

HTML/Spacebars

<template name="orders">
  <table class="table table-striped">
    <thead>
      <tr>
        <th>
          Order Number
        </th>
        <th>
          Ordered on
        </th>
        <th>
          Items in order
        </th>
      </tr>
    </thead>
    <tbody>
      {{#each orders}}
        <tr>
          <td>
            {{orderNumber}}
          </td>
          <td>
            {{createdAt}}
          </td>
          <td>
            {{productsInOrder}}
          </td>
        </tr>
      {{/each}}
    </tbody>
  </table>
</template>

JS/Helpers

Template.orders.helpers({
  'orders': function() {
    return Orders.find({});
  }
});

Rendered output

https://i.sstatic.net/Qy4VP.png Upon inspection, it seems that the 'Items in order' section is not showing the correct information as expected. Could someone kindly guide me on where I might be making an error? Thank you so much for your assistance.

Answer №1

When using producstInOrder within double-braces in Spacebars, the variable gets serialized to a string. This causes each object to be serialized as '[object Object]', which is JavaScript's way of indicating that the stored variable is an object type. To display the item number for each item in the list, you'll need to iterate over productsInOrder using another each block and render the item field:

{{#each producstInOrder}}
    {{item}}
{{/each}}

In addition, you have the option to render any other available field from the items found in productsInOrder.

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

Is it possible to solve a mathematical equation entered into a text box that resembles an Excel cell, complete with an equal sign?

On my php website, I have text boxes within a form. I'm looking for a way to enter formulas like "=4*6" or "=5/12*30" (without the quotes) into the text box and have it calculate the result. I have an onchange event set up on the text box to trigger a ...

Passing PHP information into a JavaScript array

I am facing an issue with my PHP file where I am fetching data from a MySQL database and storing it in a PHP array. I am then trying to output this data as a JS array but for some reason, I am unable to access the JS variable in my JS files. Here is the c ...

Creating an object key using a passed literal argument in TypeScript

Can the following scenario be achieved? Given an argument, how can we identify the object key and access it? Any potential solutions? async function checkKey(arg:'key1'|'key2'){ // fetchResult returns an object with either {key1:&apo ...

Is it possible to utilize the if/else if command within an HTML file?

I'm having trouble with using the if command in a JavaScript script within an HTML document. I've attempted various similar commands, but none seem to be working as expected. Any suggestions or solutions? ...

ReactJS has the capability to showcase two components simultaneously

I'm facing an issue with my ReactJS workspace where I have two components but only one is displaying. import React, { Component } from 'react'; import logo, { ReactComponent } from './logo.svg'; import './App.css'; impor ...

The clash of interests between jQuery and Bootstrap

I'm facing a problem with conflicting jQuery and Bootstrap files in my header.php code. Whenever I include the jQuery file before the bootstrap.js file, it causes issues with the tabs on my website where clicking on them does not navigate me to the co ...

The PHP shopping cart file is returning an undefined JSON response

Currently, I am developing a shopping cart system using a combination of PHP and JavaScript with an xmlhttprequest call to handle the AJAX functionality. The issue I am facing is that whenever the user clicks on the 'add to cart' button, the prod ...

Is there a way to extract a value from the JWT header and store it in a variable within a nodejs application?

I have developed an API server that enables users to log in and access specific information. Upon logging in, a JWT token containing the username is generated, which I aim to store in a variable for future use. Below is the code snippet I utilize to authen ...

transforming blog into a data URL

I've been struggling to convert an image blob into a data URL using Vue.js, but I keep encountering this error: 'Error in v-on handler: "TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provide ...

How can I prevent the jQuery animation from moving elements by adjusting the border?

I've been working on a small jQuery script that animates the border of images when hovered over. However, I've run into an issue with unwanted repositioning of adjacent images due to the border width increase. $(document).ready(function(e) { ...

The configuration error occurred for the `get` action due to an unexpected response. Instead of an object, an array was received

Despite numerous attempts, I am struggling to find a solution that works for me. In my Courses controller, I am using the Students service and Staff service to access my staff and student objects. My goal is to retrieve the staffs and students objects in o ...

Utilizing ng-model to bind object values from radio inputs

Dealing with a JSON file: $scope.favoriteThings = [ {nr: 1, text: "Raindrops on roses"}, {nr: 2, text: "Whiskers on kittens"}, {nr: 3, text: "Bright copper kettles"}, {nr: 4, text: "Warm woolen mittens"}, {nr: 5, text: "Brown paper pac ...

Using AngularJS ng-repeat with jQuery find yields a single element result

I'm in the process of developing my very first AngularJS application, and I've run into an issue with the jQuery find function. Essentially, what I'm attempting to do is utilize a custom HTML component that contains a list of buttons generat ...

Checking TinyMCE to ensure it handles empty inputs properly

I find TinyMCE to be a highly effective WYSIWYG editor. The editor functions well, but I encounter an issue when trying to check if it is empty. Challenge I am in need of validating the content input in the TinyMCE textarea to ensure that something ha ...

Inserting a multidimensional array into a JSON structure

Currently, I am working on populating my mongodb database with data that needs to be in a specific format. var location1 = [2,3]; var location2 = []; location2.push(location1); location2.push(location1); var location3 = []; location3.push(location2); cons ...

Upon selecting the correct prompt, it fails to respond when I press enter

I attempted to use a multi-search script that I found on a website. I followed the instructions given and made the necessary changes. After saving the file as an HTML document, I opened it in Chrome. However, when I type in a word and hit enter, nothing ha ...

Jquery functions are failing to work in a fresh window

I am facing an issue with my project codes where I need to open a new window, display its contents, and then replace a specific div with some content. However, the JavaScript functions in the new window are not working. How can I enable JavaScript function ...

Utilizing Mongodb Aggregation with Node.js and Spring Boot

Is there a way to query the customers list whose date of birth (dob) day and month match the current date plus 7 days? Below is the node.js query which I am struggling to convert into Spring Boot. //node.js let date = moment(); date = date.add(7, &quo ...

When you assign a variable with a document.write() property, it becomes undefined for some reason

I'm experiencing some issues with assigning a string value to a variable using document.write, as it keeps coming out as undefined. Here's the code I'm trying: var c; c = document.write("hello world"); document.write(c); The out ...

Close the Bootstrap burger menu after clicking on a scrollspy link

Is there a way to automatically collapse the Bootstrap burger menu when clicking on a scrollspy link? While browsing my website on a mobile device, the Bootstrap navigation menu switches to a burger icon. However, when you click on an internal link that l ...