Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoose .populate() only showing 1 document
    text
    copied!<p>I am trying to output just the hometeam name's to the page so that I can try to understand how to work with my code better. It is only printing one team to the page, and it is printing all the details of that team to the page, whereas I only want it to print one part.</p> <p>This is my code, I want it to print the name's of each <code>hometeam</code> to the page</p> <pre><code> app.get('/home', function(req, res) { Match.findOne({}).populate('hometeam.name').exec(function(err, teams){ util.log(teams); res.send(teams); }); }); </code></pre> <p>But when I load the page all I get is the first piece of data from this list of Matches</p> <pre><code>[ { "hometeam": "5106e7ef9afe3a430e000007", "_id": "5113b7ca71ec596125000005", "__v": 0, "key": 1360246730427 }, { "hometeam": "5113c13e0eea687b28000001", "_id": "5113e951354fe70330000001", "__v": 0, "key": 1360259409361 }, { "hometeam": "5113c13e0eea687b28000001", "_id": "5113e999354fe70330000002", "__v": 0, "key": 1360259481412 } ] </code></pre> <p>Also, if I try to put <code>util.log(teams.hometeam.name)</code> I get the following:</p> <pre><code>TypeError: Cannot call method 'toString' of undefined </code></pre> <p>But I would want it to be able to print the name which belongs to hometeam here. As hometeam is just the objectId of a Team in my database, am I missing something with the DBreferencing here?</p> <p>Update:</p> <p>Team Schema</p> <pre><code>var Team = new Schema({ 'key' : { unique : true, type : Number, default: getId }, 'name' : { type : String, validate : [validatePresenceOf, 'Team name is required'], index : { unique : true } } }); module.exports.Schema = Team; module.exports.Model = mongoose.model('Team', Team); </code></pre> <p>Match Schema</p> <pre><code>var Team = require('../schemas/Team').Schema; var Match = new Schema({ 'key' : { unique: true, type: Number, default: getId }, 'hometeam' : { type: Schema.ObjectId, ref: 'Team' }, 'awayteam' : { type: Schema.ObjectId, ref: 'Team' } }); module.exports = mongoose.model('Match', Match); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload