ノードで REST API を構築していますが、サブドキュメントを GET 要求に含めようとして行き詰っています。たとえば、現在、People と Locations という 2 つのスキーマがあります。1 つの場所に複数の人がいる場合もあれば、1 人が 1 つの場所を持つ場合もあります。そのため、/people を返すときに、場所のエントリに場所情報を含めたいと思います。
私の問題は、次の 2 つのいずれかであると考えています。私はノードにかなり慣れていないので、スキーマが相互に認識できるかどうか 100% 確信が持てません。Web で一般的な方法を試してみると、場所フィールドに null が入力されます。私がそれをどのように理解するかは、場所IDを人のスキーマに保存し、サブドキュメントを使用してそのIDを持つ場所を見つけて情報を入力します。
また、populate 関数の使用方法や、GET 呼び出しに対する応答を正確に記述する方法についても 100% 確信が持てません。以下の私のコードは、あなたが言わなければならないことを聞きたいです!
app.js
// Node Setup
var application_root = __dirname,
express = require('express'),
path = require('path'),
mongoose = require('mongoose'),
http = require('http');
var app = express();
// MongoDB Connection
var dbLocalhost = mongoose.createConnection('mongodb://localhost/lantern/');
// Configure Node
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(application_root, "public")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.port = 3000;
});
// Routes
var Locations = require('./routes/locations')(app, { 'mongoose': mongoose, 'db': dbLocalhost });
var People = require('./routes/people')(app, { 'mongoose': mongoose, 'db': dbLocalhost });
// Start the server
app.listen(app.port);
routes/people.js - すべてのエンドポイントがありますが、今のところ GET だけに関心があります
module.exports = function (app, options) {
var mongoose = options.mongoose;
var Schema = options.mongoose.Schema;
var db = options.db;
var PeopleModel = require('../schemas/peopleSchema')(db);
app.get('/people', function (req, res) {
return PeopleModel.find(function (err, obj) {
if (!err) {
return res.send(obj);
} else {
return res.send(err);
}
});
});
};
schemas/peopleSchema.js (「場所」フィールドは入力したいものです)
module.exports = function(db) {
return db.model('People', PeopleSchema());
}
function PeopleSchema () {
var Schema = require('mongoose').Schema;
return new Schema({
first_name: String,
last_name: String,
address: {
unit: Number,
address: String,
zipcode: String,
city: String,
region: String,
country: String
},
image: String,
job_title: String,
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null },
hourly_wage: Number,
location: [{type: Schema.ObjectId , ref: 'Locations'}], // Inheirit store info
employee_number: Number
}, { collection: 'people' });
}
schemas/locationsSchema.js - 念のため場所スキーマ
module.exports = function(db) {
return db.model('Locations', LocationsSchema());
}
function LocationsSchema () {
var Schema = require('mongoose').Schema;
return new Schema({
title: String,
address: {
unit: Number,
address: String,
zipcode: String,
city: String,
region: String,
country: String
},
current_manager: String, // Inherit person details
alternate_contact: String, // Inherit person details
hours: {
sunday: String,
monday: String,
tuesday: String,
wednesday: String,
thursday: String,
friday: String,
saturday: String,
holidays: String
},
employees: "", // mixin employees that work at this location
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null }
}, { collection: 'locations' });
}