2
我试图在我的数据库中使用新的模式,但在尝试实例化时遇到错误。我有两个其他模式(在“模型”文件夹中的两个不同模型文件中),这是完美的,并且它们以相同的方式成形。错误信息是什么意思,我能做些什么不同以防止它发生?CALL_NON_FUNCTION_AS_CONSTRUCTOR(native)
我不认为它在控制器中的其他代码的任何问题,因为我试图在相同的地方使用相同的语法实例化另一个数据库模型,并且工作正常。
我得到的错误: 500类型错误:对象不是在Schema.CALL_NON_FUNCTION_AS_CONSTRUCTOR(原生)
对不起,下面的所有代码的函数 。在这种情况下,我不知道我能排除什么。 无论如何,在此先感谢!
控制器文件:
module.exports = function(app, service) {
var imageModel = service.useModel('image');
app.post('/file-upload', function(req, res, next) {
// other code...
var imageAdd = new imageModel.ImgSchema();
}
}
MongoDB的模型(模型/ image.js):
module.exports = function (mongoose) {
var modelObject = {};
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var ImgSchema = new Schema({
name : String,
size : Number,
type : String
});
modelObject.ImgSchema = ImgSchema;
modelObject.Images = mongoose.model('Images', ImgSchema);
return modelObject;
};
MongoDB的我使用一个服务文件(service.js):
var environment;
var mongoose = require('mongoose');
module.exports.init = function(env, mongoose) {
environment = env;
mongoose = mongoose;
};
module.exports.useModel = function (modelName) {
var checkConnectionExists = (mongoose.connection.readyState === 1 || mongoose.connection.readyState === 2);
if(!checkConnectionExists)
mongoose.connect(environment.db.URL);
return require("./models/" + modelName)(mongoose);
};
module.exports.useModule = function (moduleName) {
return require("./modules/" + moduleName);
};