node.js - How can I use ng-repeat to display data stored inside a nested object? -
i working document contains nested data. i've figured out how store nested data (e.g., comments associated post), can't figure out how access data in query. here have:
node.js
app.get('post/:post_id/comments', function(req, res) { var post = require('./models/post'); post.find( {_id: req.params.post_id}, null, {}, function (err, data) { if (err) return console.error(err); return res.json(data); } ); });
mongoose:
var mongoose = require('mongoose'); var postschema = mongoose.schema({ name : string, post : string, comments : [{ name : string, text : string }] }); module.exports = mongoose.model('posts', postschema);
angularjs:
$scope.getpostcomments = function(postid){ $http({ url: 'post/'+postid+'/comments', method: "get" }) .success(function (data, status, headers, config) { $scope.comments = data.comments; console.log(data.comments); // shows "undefined" in console }) .error(function (data, status, headers, config) { $scope.status = status; }); };
html:
<div ng-repeat="comment in comments"> {{comment.name}}<br> {{comment.text}} </div>
the problem appears in $scope.comments = data.comments;
, can figure out how fix ng-repeat
display comments (instead of being blank). ideas?
the correct solution use following on returned data:
$scope.comments = data[0].comments;
Comments
Post a Comment