gpt4 book ai didi

node.js - 如何在 mongoose 中注册和调用 schema

转载 作者:太空宇宙 更新时间:2023-11-03 23:04:08 27 4
gpt4 key购买 nike

我为我的文件创建了架构并按如下方式调用它,但它显示错误,架构未注册注释......这是由于路径所致吗?

我的架构,

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);
});
};

我为我的文件创建了架构并按如下方式调用它,但它显示错误,架构未注册评论......任何人都可以建议帮助,.............. ............

最佳答案

像下面这样导出你的模型,然后在路由文件或调用文件中需要

module.exports = mongoose.model('comments', Commentsscheme);

现在需要它并用于保存评论。

关于node.js - 如何在 mongoose 中注册和调用 schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40056687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com