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