Dengan asumsi bahwa dua kode di atas berada dalam dua file yang berbeda dan dalam folder yang sama.nama file dan Skema adalah 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);
dan di file js lain Anda akan menggunakan skema ini sebagai berikut
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);
});
};