2

I'm writing a few applications that use the same DB and schemas. I need to share the mongoose model between all application- each application has her own express server.

I tried to create a models server and requesting the JSONs useing mongoose-gen npm, it worked but not completely so I have to look for another solution.

I found some answers here about creating an npm package for that, I did so but still, something is missing-I published the package and imported it to my code, but when I actually try to query the schema-nothing is happened, and the server returns 500 with the error:

MongooseError: Operation examples.insertOne() buffering timed out after 10000ms.

I think that something is missing in my package but I don't know what.

This is what the package includes:

./index.js

    const example =require("./SystemWave")
    
    module.exports.Example=example

./SystemWave.js

    const mongoose = require('mongoose')

    const systenWaveSchema = mongoose.Schema({
        timestamp: { type: Date, default: Date.now },
        subject: {type:String, require: true},
    
    })
    
    module.exports = mongoose.model('example', systenWaveSchema)

./package.json

    {
      "name": "modelsnpm",
      "version": "1.0.6",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "mongoose": "^5.11.15"
      }
    }
./node_modules

./package-lock.json

./README.md

I don't know if I should add the DB URL or anything else.

Update :

I found another solution here:

Schemas in external module not working in Node.js

but it seems to return the same error...

  • Can you share the code for the mongdb connection? It seems there is some issue with your mongo connection or your port is not open in Firewall. – Apoorva Chikara Feb 08 '21 at 13:39
  • @ApoorvaChikara the mongodb connection is in the consumer application server as if the schema was declared there. – shir Nassim Feb 09 '21 at 07:15
  • check this : https://stackoverflow.com/questions/65150651/mongoose-schema-does-not-work-when-i-try-to-save-it – Apoorva Chikara Feb 09 '21 at 07:28
  • I know how to connect my server to mongoose. The problem is connecting with external npm package that defined external models. I'm not sure if I should have a connection URL in the external package or in the consumer application. @ApoorvaChikara – shir Nassim Feb 09 '21 at 08:13

1 Answers1

1

Eventually, I came up with this solution:

Schemas in external module not working in Node.js

With a few changing in the code, it worked!

The changes I made:

const user=require('./models/user');
   
 used in the schema file
    this.models = {
        user: user
    }

Hope it could help any of you :)