0

我正在节点中构建一个 REST api,我一直试图将子文档包含到我的 GET 请求中。例如,我现在有两个架构,人员和位置。一个位置可以有很多人,一个人可以有一个位置。因此,当您返回 /people 时,我希望我的位置条目包含位置信息。

我相信我的问题可能是两件事之一。我对节点相当陌生,所以我不能 100% 确定我的模式是否可以互相看到,当我尝试网络上的常用方法时,我的位置字段被填充为 null。我如何理解它,我将位置 ID 存储在我的人员模式中,然后使用子文档它会找到具有该 ID 的位置并填写信息。

我也不是 100% 确定如何使用填充函数,以及我应该如何编写对我的 GET 调用的响应。下面是我的代码,我很想听听你要说什么!

应用程序.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' });
}
4

0 回答 0