上記の2つのコードが2つの異なるファイルにあり、同じフォルダーにあると仮定します。andSchemaファイル名はcomment.js
var mongoose = require('mongoose'),
path = require('path'),
config = require(path.resolve('./config/config')),
Schema = mongoose.Schema;
var Commentsscheme = new Schema({
articleid: {
type: Schema.ObjectId
},
fromuser: {
type: String
},
touser: {
type: String
},
comment: {
type: String
}
});
module.exports = mongoose.model('Comment', Commentsscheme);
他のjsファイルでは、このスキーマを次のように使用します
var path = require('path'),
mongoose = require('mongoose'),
passport = require('passport'),
// here you need to put the path/name of the file so that module will load.
Comments = require('comment.js');
/* ------ Inserting a comment ------ */
exports.insertcomment = function (req, res) {
var comments = new Comments(req.body);
console.log(comments)
comments.status = 1;
var data = {};
comments.save(function (err,resl) {
if (err) {
console.log(err);
return err;
}
data = { status: false, error_code: 0, message: 'Unable to insert' };
if (resl) {
data = { status: true, error_code: 0,result: resl, message: 'Inserted successfully' };
}
res.json(data);
});
};