【发布时间】:2018-11-16 00:54:01
【问题描述】:
我在hapiJS 和mongoDB 有两个项目。一个用于管理员用户,另一个用于普通用户。我正在尝试从 admin 用户 访问一个在 普通用户 中定义的数据库集合。运行项目时出现如下错误
{ MissingSchemaError: Schema hasn't been registered for model "Category". Use mongoose.model(name, schema)
at new MissingSchemaError (/home/jeslin/projects/hapi/gg-admin/node_modules/mongoose/lib/error/missingSchema.js:22:11)
at Mongoose.model (/home/jeslin/projects/hapi/gg-admin/node_modules/mongoose/lib/index.js:380:13)
at Object.<anonymous> (/home/jeslin/projects/hapi/gg-admin/app/helpers/dashboard.js:7:27)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/jeslin/projects/hapi/gg-admin/app/controllers/web/dashboard.js:2:25)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3) message: 'Schema hasn\'t been registered for model "Category".\nUse mongoose.model(name, schema)', name: 'MissingSchemaError' }
我的控制器:
exports.showAllCategory = {
description: 'returns all caregories',
auth: {
mode: 'try',
strategy: 'standard'
},
handler: async (request,h) => {
try {
if (request.auth.isAuthenticated) {
var userDetails = request.auth.credentials;
let category = await dashboardHelper.fetchAllCatogeryItems();
if (category.statusCode === 200) {
return h.view('dashboard/dashboard', {user: userDetails, categoryData: category.categoryData});
}
}
} catch (error) {
return h.redirect('/dashboard');
}
}
};
我的数据库操作位于helper.js 文件:
'use strict';
const Mongoose = require('mongoose');
const Category = Mongoose.model('Category');
const Joi = require('joi');
exports.fetchAllCatogeryItems = async() =>{
return new Promise(async (resolve, reject) =>{
try {
let category = await Category.find();
if (categoryData) {
return resolve({
statusCode: 200,
categoryData: category
})
} else {
statusCode: 400
}
} catch (error) {
return reject(error)
}
});
};
我已将两个应用程序的 MONGO_URL 环境连接到同一个 mongodb,并且可以访问 admin 用户模块
中定义的models
我知道我的代码不会像我的
访问Category模型在 普通用户并尝试以管理员用户
是否可以访问普通用户项目中定义的模型?请帮我找到解决这个问题的方法。我为此浪费了将近 2 天的时间。
我也没有经验nodejs (hapijs)
【问题讨论】: