如何在猫鼬中注册和调用模式 [英] How to register and call a schema in mongoose

查看:66
本文介绍了如何在猫鼬中注册和调用模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created schema for my file and calling it as below but it says error that schema is not registered for comments........Is this due to path?

我的模式,

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

我为我的文件创建了一个架构,并按如下方式调用它,但是它指出未注册架构以进行注释的错误........任何人都可以建议帮助..... ..............

I created schema for my file and calling it as below but it says error that schema is not registered for comments........can any one please suggest help,........................

推荐答案

按如下所示导出模型,然后在路由文件或调用文件中进行输入

Export your model like below and then require in route file or calling file

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

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

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

Now require it and use to save comments.

这篇关于如何在猫鼬中注册和调用模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆