0
我在获取模型中的嵌套对象时遇到了问题。 我有一家模特餐厅,我参考了一篇文章。这只是一个演示,我来测试,这是如何工作的,但我没有得到它.. :(使用MongoosJS,NodeJS和AngularJS填充嵌套对象
啊..我用meanjs ..
这里是我的餐厅模型..
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Restaurant Schema
*/
var RestaurantSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Restaurant name',
trim: true
},
desc: {
type: String,
default: 'description is here'
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
article: {
type: Schema.ObjectId,
ref: 'Article'
}
});
mongoose.model('Restaurant', RestaurantSchema);
这是我第模型
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
content: {
type: String,
default: '',
trim: true
}
});
mongoose.model('Article', ArticleSchema);
这两种方法都在后台的NodeJS餐厅控制器:
exports.list = function(req, res) { Restaurant.find().sort('-created').populate('article').exec(function(err, restaurants) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(restaurants);
}
});
};
exports.restaurantByID = function(req, res, next, id) { Restaurant.findById(id).populate('article').exec(function(err, restaurant) {
if (err) return next(err);
if (! restaurant) return next(new Error('Failed to load Restaurant ' + id));
req.restaurant = restaurant ;
next();
});
};
然后,我有一个角度控制器的方法来安全的新餐厅和文章,这是工作。当我访问"http://localhost:3000/#!/articles"
时。但是,当我试图拿出例如标题,它不起作用。
我创造我的餐厅和文章这样说:
// Create new Restaurant
$scope.create = function() {
// Create new Restaurant object
var newArticle = new Articles({
title: this.articleTitle
});
newArticle.$save();
var restaurant = new Restaurants({
name: this.name,
desc: this.description,
article: newArticle._id
});
// Redirect after save
restaurant.$save(function (response) {
$location.path('restaurants/' + response._id);
// Clear form fields
$scope.name = '';
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};
这是我创造的观点:
<div class="controls">
<input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
<input type="text" data-ng-model="articleTitle" id="article" class="form-control" placeholder="artikel" required>
<input type="text" data-ng-model="description" id="description" class="form-control" placeholder="desription" required>
</div>
,这是在我的列表视图
<h4 class="list-group-item-heading" data-ng-bind="restaurant.name"></h4>
<h3 class="list-group-item-heading" data-ng-bind="restaurant.article"></h3>
<h3 class="list-group-item-heading" data-ng-bind="restaurant.desc"></h3>
的描述在浏览器中可见,但对文章没有任何可见的地方。我如何获得有关餐馆对象中文章的信息?
也许,这是很容易的,但我没有发现它。在先进
感谢..
Interresting的事情也是,我没有得到文章的ID或者.. – damir 2014-10-08 15:50:30