我正在尝试将我的mlab与我的模型联系起来,但它给我一个错误:
throw new TypeError('Undefined type
'+ name +'at
'+ path +^
TypeError:
undefined
处的未定义类型required
你尝试过嵌套Schemas吗?您只能使用refs或数组进行嵌套。
在我的文件夹模型中,我有:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ProjectSchema = new Schema({
name: String,
email: String,
description: String,
image: {data: Buffer, contentType: String },
date: { type: Date, default: Date.now },
location: String, //{street: String, number: String, zip: String, city: String, required: true},
fixedAmount: Number, required: true,
wantedAmount: Number,
receivedAmount: Number
});
module.exports = mongoose.model('Project', ProjectSchema);
和我的UserSchema差不多......
我还有一个 index.js :
const mongoose = require('mongoose');
module.exports.connect = (uri) => {
mongoose.connect(uri);
// plug in the promise library:
mongoose.Promise = global.Promise;
mongoose.connection.on('error', (err) => {
console.error(`Mongoose connection error: ${err}`);
process.exit(1);
});
// load models
require('./user');
require('./project');
require('./common');
};
在我的server.js中:
const config = require('./config.js');
// connect to the database and load models
require('./server/models').connect(config.dbUrl);
并在我的 config.js:
中const dotenv = require('dotenv').config();
module.exports = {
'port': process.env.PORT || 5000,
'dbUrl': `mongodb://${process.env.USER_DB}:${process.env.PASSWORD_DB}@ds123930.mlab.com:23930/kickass`,
"jwtSecret": "a secret phrase !"
}