Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的Mongoose findAll方法返回500错误。。?_Javascript_Node.js_Mongodb_Mongoose_Mongoose Schema - Fatal编程技术网

Javascript 为什么我的Mongoose findAll方法返回500错误。。?

Javascript 为什么我的Mongoose findAll方法返回500错误。。?,javascript,node.js,mongodb,mongoose,mongoose-schema,Javascript,Node.js,Mongodb,Mongoose,Mongoose Schema,这是我的模型/模式。create方法有效,但“all”方法无效。。它返回一个500 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var DiSchema = new mongoose.Schema({ name: { type: String, lowercase: true , required: true }, photo: { type: String }, email: { type:

这是我的模型/模式。create方法有效,但“all”方法无效。。它返回一个500

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var DiSchema = new mongoose.Schema({
  name: { type: String, lowercase: true , required: true },
  photo: { type: String },
  email: { type: String, unique: true, lowercase: true },
  year: { type: Number},
  timestamp: { type : Date, default: Date.now },
  description: { type: String},
  location: { type: Array },
  social: {
    website: {type: String},
    facebook: {type: String },
    twitter: {type: String },
    instagram: {type: String }
  }
});

DiSchema.methods.create = function(o, cb){
  this.model.save(o, cb);
};

DiSchema.methods.all = function(o, cb){
  Di.find(o, cb);
};

module.exports = mongoose.model('Di', DiSchema);
这是我的控制器:

'use strict';

var Di = require('../models/di');

exports.create = function(req, res){
  Di.create(req.body , function(err, di){
    console.log('req.body.di', req.body);
    res.send({di:di});
  });
};

exports.index = function(req, res){
  Di.all({}, function(err, dis){
    res.send({dis:dis});
  });
};
路线:

var dis = require('../contollers/dis');
app.get('/dis', dis.index);
app.post('/dis', dis.create);
create方法工作得很好,但是当我使用index/all方法到达get端点时,我得到了500

我可以将查询直接放在控制器中,它可以工作

但是,当我试图从schema方法调用它时,它不会

我使用的方法错了吗?我的JS关机了吗


更新:

如果我将此代码放入控制器中,它会工作:

exports.index = function(req, res){
  Di.find({}, function(err, dis) {
    if (!err){ 
        res.send(dis);
  };
});
};
仍然对最初的问题“为什么该方法不能从模型中工作”感兴趣


我也在模型中尝试过这种重构,但没有成功:

DiSchema.methods.list = function(o, cb){
  this.model.find(o, cb);
};
&


好吧,有几件事

需要将模型更改为静态方法。模型的一种方法

此外,语法也需要更改

DiSchema.statics.all = function(o, cb){
  this.model('Di').find(o, cb);
};

如果有人想用外行的话更好地描述为什么需要这两个改变——我很乐意改变正确答案的所有权

导致500错误的异常包含哪些内容?你应该可以从那里调试它。我没有在inspector tools的网络上看到任何东西。唯一能显示500的是morgan在候机楼。我将进一步研究调试这个。如果我将console.log放在蒸馏器.all函数中(在控制器中),它永远不会被命中,因此该函数似乎没有被正确调用或挂起。。。
DiSchema.statics.all = function(o, cb){
  this.model('Di').find(o, cb);
};