0

[解決済み - モデルのばかげたミス]

私はmongooseとexpressjsを参照して入力しようとしましたが、まったく機能しません。私はこれらに従いました: 1. Mongoose の例 2. Mongoose populate

次の 3 つのファイルを作成しました。

1) モデル/profile.js

var mongoose = require('mongoose'), Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var profileSchema = new Schema({
    name: String
});
module.exports = mongoose.model('Profile', profileSchema);

2) モデル/work.js

var mongoose = require('mongoose'), Schema = mongoose.Schema, ObjectId = Schema.ObjectId;

var workSchema = new Schema({
    title: String,
    creditsFor: [{type: Schema.Types.ObjectId, ref: 'profile'}],
});
module.exports = mongoose.model('Work', workSchema);

3) コントローラー/work.js

var Work = require('../models/work.js'), fs = require('fs'), mongoose = require('mongoose');
......
exports.create = function(req, res) {
    credits = [];
    credits.push(mongoose.Types.ObjectId('5174a9ec993af25b01000003')); // I already created a 'Profile' and copy-pasted the _id

    var work = {
    title: req.body.title,
            creditsFor: credits
    }   

    var workObj = new Work(work);

    workObj.save(function(err, data) {
    if(err) {
        throw err;
    } else {
        req.flash('info', 'Work item saved.');
        res.redirect('/work/' + data._id);
    }
    });
}
.......
exports.show = function(req, res) {
var work = req.params.work;
Work.findOne({_id: work})
    .populate('creditsFor')
    .exec(function(err, data) {
    if(err) {
        throw err;
    } else {
        console.log(data);
        res.render('site/work/show', {work: data, message: req.flash('info')});
    }
});
}

populate() を使用すると、creditsFor が返されます。populate() にコメントすると、次のような文字列 (?) になります: { title: 'My Title', creditsFor: '5174a9ec993af25b01000003' }

何が問題なのかわかりませんでした。両方のモデルを 1 つのファイルにまとめようとしましたが、まだ何もありません。

4

0 に答える 0