我正在开发MERN Stack应用程序。类型引用驻留在不同文件中的架构时出现错误。
在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 Profile
const User = require("../../models/User");
// @route GET api/profile/test
// @desc Tests profile route
// @access Public
router.get("/test", (req, res) => res.json({ msg: "Profile Works" }));
// @route GET api/profile
// @desc Get current users profile
// @access Private
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;
运行应用程序时,出现以下错误:
devconnector777\node_modules\mongoose\lib\schema.js:663
throw new TypeError('Undefined type `' + name + '` at `' + path +
^
TypeError: Undefined type `undefined` at `title.required`
Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (C:\Users\hp-u\Desktop\devconnector777\node_modules\mongoose\lib\schema.js:663:11)
at Schema.path devconnector777\node_modules\mongoose\lib\schema.js:512:29)
at Schema.add devconnector777\node_modules\mongoose\lib\schema.js:378:12)
at Schema.add devconnector777\node_modules\mongoose\lib\schema.js:367:14)
at new Schema devconnector777\node_modules\mongoose\lib\schema.js:94:10)
at Function.Schema.interpretAsType devconnector777\node_modules\mongoose\lib\schema.js:616:27)
at Schema.path devconnector777\node_modules\mongoose\lib\schema.js:512:29)
at Schema.add devconnector777\node_modules\mongoose\lib\schema.js:378:12)
at new Schema devconnector777\node_modules\mongoose\lib\schema.js:94:10)
at Object.<anonymous> devconnector777\models\Profile.js:5:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
[nodemon] app crashed - waiting for file changes before starting...
我的models文件夹中有2个js文件,即models> User.js和models> Profile.js: 1.User.js:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
avatar: {
type: String
},
date: {
type: Date,
default: Date.now
}
});
module.exports = User = mongoose.model("users", UserSchema);
2.Profile.js:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users"
},
handle: {
type: String,
required: true,
max: 40
},
company: {
type: String
},
website: {
type: String
},
location: {
type: String
},
status: {
type: String,
require: true
},
skills: {
type: [String],
require: true
},
bio: {
type: String
},
githubusername: {
type: String
},
experience: [
{
title: {
String,
required: true
},
company: {
type: String,
required: true
},
location: {
type: String
},
from: {
type: Date,
required: true
},
to: {
type: Date
},
current: {
type: Boolean,
default: false
},
description: {
type: String
}
}
],
education: [
{
school: {
String,
required: true
},
degree: {
type: String,
required: true
},
fieldofstudy: {
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
},
facebook: {
type: String
},
linkedin: {
type: String
},
instagram: {
type: String
}
},
date: {
type: Date,
default: Date.now
}
});
module.exports = Profile = mongoose.model("profile", ProfileSchema);
我无法找出错误原因。如何解决此错误?任何帮助将不胜感激。