更新
我已将问题缩小到主要问题:
Nested Resources with ResourceJS中的示例代码,需要两个Mongoose Schema,PARENT和CHILD,&创建这些REST资源:
/parent - (GET), (POST)
/parent/:parentId - (GET), (PUT), (DELETE)
/parent/:parentId/child - (GET), (POST)
/parent/:parentId/child/:childId - (GET), (PUT), (DELETE)
PARENT和CHILD是2个型号;一个人宣布另一个的父母:
var Parent = mongoose.model('Parent', new mongoose.Schema({
name: {
type: String,
required: true
}
}));
var Child = mongoose.model('Child', new mongoose.Schema({
name: {
type: String,
required: true
},
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Parent',
index: true,
required: true
}
}));
然而,我们正尝试使用嵌入式文档实现此目标:
模型/ MyModel.js
var ChildSchema = new mongoose.Schema({
// variables and ID
}, { collection: 'child' });
var ParentSchema = new mongoose.Schema({
// variables and ID
children: [ChildSchema]
}, { collection: 'parent'});
module.exports = mongoose.model('parent', ParentSchema);
ResourceJS使用此功能创建REST接口:
Resource(app, route, name, model);
其中:
app 是Express应用程序。
路线是通往" mount"的路线这个资源上。在嵌套资源中,这可能是' / parent /:parentId'
name - 用于该资源的网址路径的资源名称。
模型 - 界面的Mongoose模型。
我们希望在 model 参数中获得类似 app.models.parent.child 的内容来制作子REST接口,或类似的东西:
Resource(app, '/child/:childId', 'child', app.models.parent.child).rest({
// the before handler code
});
显然,这不起作用。对于CHILD,我不确定我在 model 参数中输入的对象类型是什么类型。它究竟要求的是什么。
parent /:parentId / child(GET)返回[]
parent /:parentId / child /:childId(GET)返回此错误:
{" status":404,"错误":["未找到资源"]}
我们的代码:
index.js
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var methodOverride = require('method-override');
var _ = require('lodash');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(function(req, res, next) { // CORS Support
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.models = module.exports = {
parent: require('./models/MyModel')
// should Child be declared ?
};
var Resource = require('resourcejs');
// The parent REST interface.
Resource(app, '', 'parent', app.models.parent).rest();
// The child REST interface.
Resource(app, '/parent/:parentId', 'child', app.models.learn.child).rest({
// ResourceJS seems to append 'Id' to 'parent' var above
// before handler, include filter and parent info
before: function(req, res, next) {
req.body.parent = req.params.parentId;
req.modelQuery = this.model.where('parent', req.params.parentId);
next();
}
});
mongoose.connect('mongodb://localhost/myApp');
mongoose.connection.once('open', function() {
app.listen(3000);
});
功能
所有依赖项都是最新的,并且反映了ResourceJS JSON包中显示的内容: "表达":" ^ 4.14.0", " fast-json-patch":" ^ 0.5.7", " lodash":" ^ 4.17.4", " mongodb":" ^ 2.2.16", " mongoose":" ^ 4.7.5"
这是资源示例代码
// The parent REST interface.
Resource(app, '', 'parent', Parent).rest();
// The child REST interface.
Resource(app, '/parent/:parentId', 'child', Child).rest({
// Add a before handler to include filter and parent information.
before: function(req, res, next) {
req.body.parent = req.params.parentId;
req.modelQuery = this.model.where('parent', req.params.parentId);
next();
}
});