我在模型中获取嵌套对象时遇到问题。 我有一个模型餐厅,我在那里引用一篇文章。这只是一个演示,让我测试它是如何工作的,但我没有得到它.. :(
啊..我正在使用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>
该描述在浏览器中可见,但是,该文章没有任何可见的内容。如何获取餐厅对象中有关该文章的信息?
可能这很容易,但我没有找到它..
感谢先进..
答案 0 :(得分:0)
所以我想出来了......我弄错了我是如何创作这篇文章的。我觉得这篇文章有点不对劲,然后我创造了一个新的菜肴..
首先,我正在保存这道菜,在回调中,我正在创建餐厅,其中包含我从回调回复中获得的信息..这对我有用..
// Create new Restaurant
$scope.create = function () {
// Create new Restaurant object
var dishId = 0;
var thisObject = this;
var newDish = new Dishes({
name: this.dishName
});
newDish.$save(function(response){
console.log('XX in $save: %s', response._id);
dishId = response._id;
var restaurant = new Restaurants({
name: thisObject.name,
desc: thisObject.description,
art: {
title: thisObject.artTitle,
content: thisObject.artContent
},
dish: dishId
});
// Redirect after save
restaurant.$save(function (response) {
$location.path('restaurants/' + response._id);
// Clear form fields
$scope.name = '';
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
}
);
};
当然,我必须在我的服务器控制器中填充这个新菜:
exports.list = function(req, res) { Restaurant.find().sort('-created').populate('dish').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('dish').exec(function(err, restaurant) {
if (err) return next(err);
if (! restaurant) return next(new Error('Failed to load Restaurant ' + id));
req.restaurant = restaurant ;
next();
});
};
这是我的餐厅模特:
'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'
},
dish: {
type: Schema.ObjectId,
ref: 'Dish'
},
art: {
title: {
type: String,
default: 'HelloTitle',
trim: true,
required: 'Title cannot be blank'
},
content: {
type: String,
default: '',
trim: true
}
}
});
mongoose.model('Restaurant', RestaurantSchema);