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

标签 node.js mongodb express mongoose

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

我的架构,

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/

相关文章:

node.js - 为同一路线 sails 多个处理程序

node.js - IIS、Node.js 和带有 IISNode 的 Web 应用程序未使用虚拟目录正确配置

node.js - 在 NodeJS 中使用异步响应进行异步调用

ajax - 使用 Angular js 登录 ajax 后清空 Passport session

javascript - 如何使用 Handlebars 编写一行 If 语句?

Node.js 和 LÖVE2D 套接字通信

javascript - 如何在nodejs中执行mongoose查询

javascript - MongoDB 错误 'Cannot read property ' _id' of undefined'

mongodb - 1 :1 and group chat schema in NoSQL/MongoDB

javascript - 使用express.js和passport.js从POST获取表单数据