我为我的文件创建了模式,并按照下面的方式调用它,但它表示模式没有注册注释的错误........这是由于路径吗?
我的架构,
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
}
});
mongoose.model('comments', Commentsscheme);
我的js,
var path = require('path'),
mongoose = require('mongoose'),
passport = require('passport'),
Comments = mongoose.model('comments');
/* ------ 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);
});
};
我为我的文件创建了架构,并按下面的方式调用它,但是它表示架构没有注册注释的错误........任何人都可以建议帮助,.......... ..............
答案 0 :(得分:2)
假设上面两个代码在两个不同的文件中并且在同一个文件夹中。 和 架构文件名是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);
});
};
答案 1 :(得分:1)
像下面那样导出模型,然后在路径文件或调用文件
中输入module.exports = mongoose.model('comments',Commentsscheme);
现在需要它并用于保存评论。