Json data not displaying in view

Here is sample json attached for an iteration.
json.txt (5.1 KB)

and here is my loop in the view with HANDLEBARS in express js

<div>
        {{# each items }}
            <article class="item">
                <div>
				<table>
				<tr>
				<td>
				{{this._id}}  // THIS PRINTS
				</td>
				
				{{# each data}}
					<td>{{this.mPC}}</td>  // THIS DOES NOT PRINT
				{{/each}}  

				</tr>
				<table>							
				</div>              								
            </article>
        {{/each}}
    </div>

What I am doing wrong in above code so that THIS DOES NOT PRINT in the above code ?

Well the obvious question would be 'where do you define ā€œdataā€?

its in json. json attached in 1st post

So you meant to say {{# each this.data}} instead?

see…I want to print mPC in data. …whatever it takes.

could you please go through the 1st post again ? I have explained it very clearly.

<td>{{this.mPC}}</td> // THIS DOES NOT PRINT

I want to fix this

Well, fine. Here’s what i’ll tell you.

Your code works.

Your JSON is malformed.

You havent shown us the loader that actually loads your JSON.

When the JSON is fixed, and the loader is correct, your code works.

what do you mean by malformed ? what is the issue ?

This prints from that json
{{this._id}} // THIS PRINTS

This isn’t clear to me

				{{# each data}}
					<td>{{this.mPC}}</td>  // THIS DOES NOT PRINT
				{{/each}}  

Is it not printing because there is no ā€œdataā€? i.e. the <td></td> aren’t there?
Or is there no ā€œthis.mPCā€ i.e. <td></td> are there, but empty?

this is the json
json.txt (5.1 KB)

Well ignoring the strict validation that keys should be double-quote-wrapped strings

and that string values should be double-quote instead of single-quote wrapped,

your ID value is an unquoted string,

several (all?) of your floating point numbers are wrapped in quotes. Not strictly a malformation, but it might confuse some code if you try and do math with it.

you’re trying to use this JSON as a list, but the JSON isnt wrapped in a list container,

Here is the Loader in express js

var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://xx.xx.xx.xx:27017/somedb', { useMongoClient: true });


var Schema = mongoose.Schema;

var marketDataSchema = new Schema({
  marketdata: {type: String, required: true}
}, {collection: 'marketdata'});

var marketdata = mongoose.model('marketdata', marketDataSchema);



router.get('/get-data', function(req, res, next) {
  console.log("test");
 marketdata.find()
      .then(function(doc) {
		console.log(doc);
		  
         res.render('index', {items: doc});
      });
});

Yeah, see, the JSON you provided us starts with a {, not a [. So items is not a list, it’s an object.

Here is your code, working correctly, with a properly formatted JSON. I have made no modifications to the template, just added some CSS so i can actually see the resulting cells clearly.

So you may need to examine what ā€˜doc’ is (console.log(doc)) before it gets handed to the template engine.

[ is there.

console.log(doc) in the above server code prints like this attached.
test.txt (978 Bytes)

and

your ID value is an unquoted string,

its mongodb ID value.

and
<td> {{this._id}} // THIS PRINTS </td>

its the data part not getting printed

1 Like

got this issue fixed.
It was issue with mongoose schema.

2 Likes

Hey much thanks for solution bro. I was facing this problem for many days in Wordpress. I read many forums about the solution regarding the schema. I don’t know about mongoose schema before but I must say alot of thanks for your efforts. Also, my site was having some issues which got fixed.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.