尝试在路由文件中要求我的Mongoose Schema时,我收到一个未定义的错误。我在Profile.js中创建了Schema,我将在profile.js中使用Express。我知道之前有过这方面的问题,但没有任何建议会为我解决。
Profile.js (我的架构已创建)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
handle: {
types: String,
required: true,
max: 40
},
company: {
type: String
},
website: {
type: String
},
region: {
type: String,
required: true
},
status: {
type: String,
required: true
},
skills: {
type: [String]
},
bio: {
type: String
},
githubusername: {
type: String
},
experience: [
{
title: {
type: String,
required: true
},
company: {
type: String,
required: true
},
from: {
type: Date,
required: true
},
to: {
type: Date
},
current: {
type: Boolean,
default: false
},
description: {
type: String
}
}
],
social: {
youtube: {
type: String
},
twitter: {
type: String
},
twitch: {
type: String
},
telegram: {
type: String
},
discord: {
type: String
}
},
date: {
type: Date,
default: Date.now
}
});
module.exports = Profile = mongoose.model('profile', ProfileSchema);
profile.js (我要求的地方)
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const passport = require('passport');
// Load Profile Model
const Profile = require('../../models/Profile');
// Load User Model
const User = require('../../models/User');
router.get('/test', (req, res) => res.json({ msg: 'Profile Works' }));
router.get(
'/',
passport.authenticate('jwt', { session: false }),
(req, res) => {
const errors = {};
Profile.findOne({ user: req.user.id })
.then(profile => {
if (!profile) {
errors.noprofile = 'There is no profile for this user';
return res.status(404).json(errors);
}
res.json(profile);
})
.catch(err => res.status(404).json(err));
}
);
module.exports = router;
错误
C:\Users\Razer\Desktop\MCREP\node_modules\mongoose\lib\schema.js:626
throw new TypeError('Undefined type `' + name + '` at `' + path +
^
TypeError: Undefined type `undefined` at `handle.required`
Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (C:\Users\Razer\Desktop\MCREP\node_modules\mongoose\lib\schema.js:626:11)
at Schema.path (C:\Users\Razer\Desktop\MCREP\node_modules\mongoose\lib\schema.js:483:29)
at Schema.add (C:\Users\Razer\Desktop\MCREP\node_modules\mongoose\lib\schema.js:364:12)
at Schema.add (C:\Users\Razer\Desktop\MCREP\node_modules\mongoose\lib\schema.js:353:14)
at new Schema (C:\Users\Razer\Desktop\MCREP\node_modules\mongoose\lib\schema.js:88:10)
at Object.<anonymous> (C:\Users\Razer\Desktop\MCREP\models\Profile.js:5:23)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\Razer\Desktop\MCREP\routes\api\profile.js:7:17)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
答案 0 :(得分:0)
您在
中写了类型handle: {
types: String,
required: true,
max: 40
},
尝试将其更改为类型