【问题标题】:fetch data from one mongodb collection in multiple projects从多个项目中的一个 mongodb 集合中获取数据
【发布时间】:2018-11-16 00:54:01
【问题描述】:

我在hapiJSmongoDB 有两个项目。一个用于管理员用户,另一个用于普通用户。我正在尝试从 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)

【问题讨论】:

    标签: node.js mongodb hapijs


    【解决方案1】:

    你可以使用这个 npm 包The official MongoDB driver for Node.js. 来查询你的 mongodb 而无需定义架构。我希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 2017-04-03
      • 2019-03-20
      • 2010-12-12
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多