My Object is like,
{
"Team-A": {
"captain": "A",
"homeGround": "some ground",
"winLoss": "12-4-0"
},
"Team-B": {
"captain": "B",
"homeGround": "some ground 2",
"winLoss": "4-4-4"
}
}
I need to display this information as,
<h1>Team-A</h1>
<h2>some ground</h2>
<h3>12-4-0</h3>
<h1>Team-B</h1>
<h2>some ground2</h2>
<h3>4-4-4</h3>
Nodejs:
MongoClient.connect("mongodb://localhost:27017/MY_DB_TEST", function(err, db) {
if(!err) {
console.log("We are connected");
var collection = db.collection('football_teams');
var stream = collection.find().stream();
stream.on("data", function(item){
console.log(item);
res.render('index', {obj: item });
});
}
});
Jade:
h1= obj.captain
h2= obj.homeGround
h3= obj.winLoss
My console output is:
{ _id: 51064fa1e0d5d118b4b29aa0,
'Team-A': {
'captain": 'A',
'homeGround": 'some ground',
'winLoss': '12-4-0'
} }
title is not defined
at eval (eval at <anonymous> (C:\node_modules\jade\lib\jade.js:176:8))
at exports.compile (C:\node_modules\jade\lib\jade.js:181:12)
at Object.exports.render (C:\node_modules\jade\lib\jade.js:216:14)
at View.exports.renderFile [as engine] (C:\node_modules\jade\lib\jade.js:243:13)
at View.render (C:\node_modules\express\lib\view.js:75:8)
at Function.app.render (C:\node_modules\express\lib\application.js:503:10)
at ServerResponse.res.render (C:\node_modules\express\lib\response.js:721:7)
at CursorStream.exports.index (C:\Users\HFR&D\Desktop\nodemongoexpress\routes\index.js:18:8)
at CursorStream.EventEmitter.emit (events.js:96:17)
at CursorStream._onNextObject (C:\Users\HFR&D\Desktop\nodemongoexpress\node_modules\mongodb\lib\mongodb\cursorstream
I'm not understanding how I should loop through my object and get the values Team-A/Team-B and their respective details.
How do I achieve this using nodejs and jade.
UPDATE:
Say there are 20 teams, my collection will consists of 20 documents. What I want to do is, loop through my collection and display info in the 20 documents as described above.
Is my db structure correct? Should I have a different document for each team?
If yes, how do I get the title of the documents (team-A, team-B) dynamically while rendering in jade?
You do get the Team-A collection as you see it in your console.log
{ _id: 51064fa1e0d5d118b4b29aa0,
'Team-A': {
'captain": 'A',
'homeGround": 'some ground',
'winLoss': '12-4-0'
} }
The error you get is title is not defined, so I am guessing something else is breaking your code. (Perhaps the title variable for the layout is not set?)
After you fix that problem do like so
h1= obj['Team-A'].captain
h2= obj['Team-A'].homeGround
h3= obj['Team-A'].winLoss
That will give you the data.
var myobj = {
"Team-A": {
"captain": "A",
"homeGround": "some ground",
"winLoss": "12-4-0"
},
"Team-B": {
"captain": "B",
"homeGround": "some ground 2",
"winLoss": "4-4-4"
}
};
var key = Object.keys(obj)[0] // gives you the first key i.e. Team-A. its an array so every key is easily accessible.
var captain = myobj[key].captain // gives 'A'
var homeGround = myobj[key].homeGround // gives 'some ground'
var winloss = myobj[key].winloss //gives "12-4-0"