Mongoose - TypeError:object不是函数

时间:2021-10-24 15:47:14

I am trying to export a Mongoose model from my model/user.model.js file to my server.js file, in my server directory.

我正在尝试将myo模型/ user.model.js文件中的Mongoose模型导出到我的server.js文件中,在我的服务器目录中。

model/user.model.js

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

var UserSchema = new Schema({
    instagramId: { type: String, index: true },
    email: { type: String, unique: true, lowercase: true },
    password: { type: String, select: false },
    userName: String,
    fullName: String,
    picture: String,
    accessToken: String
});

module.exports = mongoose.model('User', UserSchema, 'users');

server.js

var User = require('./models/user.model');

mongoose.connect(config.db);

I get this error message

我收到此错误消息

\server\models\user.model.js 5

var UserSchema = new Schema({

var UserSchema = new Schema({

TypeError: object is not a function

TypeError:object不是函数

I know I declared my schema as UserSchema, however I thought I exported the file with the variable User

我知道我将我的架构声明为UserSchema,但我认为我使用变量User导出了该文件

module.exports = mongoose.model('User', UserSchema, 'users');

I'm trying to use the name User to query my mongoose model.

我正在尝试使用User这个名称来查询我的猫鼬模型。

Any ideas? Thanks in advance.

有任何想法吗?提前致谢。

1 个解决方案

#1


10  

You are mistakenly assigning Schema to an instance:

您错误地将Schema分配给实例:

var Schema = mongoose.Schema();

Instead, it should be assigned the mongoose.Schema class itself:

相反,它应该被赋予mongoose.Schema类本身:

var Schema = mongoose.Schema;

#1


10  

You are mistakenly assigning Schema to an instance:

您错误地将Schema分配给实例:

var Schema = mongoose.Schema();

Instead, it should be assigned the mongoose.Schema class itself:

相反,它应该被赋予mongoose.Schema类本身:

var Schema = mongoose.Schema;