简体   繁体   中英

fetch data from one mongodb collection in multiple projects

I have two projects in hapiJS and mongoDB . One is for admin user and another for normal user . I am trying to access a db collection from admin user which is defined in the normal user . The following error is generated when I run the project

{ 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' }

my controller :

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');
        }
    }
};

and my db actions are at following helper.js file :

'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)
        }
    });
};

I have connected the MONGO_URL environment for both apps to point at the same mongodb, and can access the models defined within admin user module

I know that my code won't work as my Category model has defined in normal user and trying to access at admin user

Is it possible to access the models defined in normal user project somebody ?? Please help me to find a solution for this problem . I have wasted almost 2 days for this. also I am not experienced in nodejs ( hapijs )

you can use this npm package The official MongoDB driver for Node.js. for querying your mongodb without defining the schema.I hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM