throw new Error('Mongoose is not connected');









up vote
2
down vote

favorite












So I'm trying to use the mongoose-gridfs module with express but I get the following error when I try to run my express server.



Error: Mongoose is not connected 
at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
_modulesmongoose-gridfslibstorage.js:43:11)
at module.exports (C:UsersBARMANDocumentsCodeszubis-martnode_mo
dulesmongoose-gridfslibstorage.js:472:18)
at Object.<anonymous> (C:UsersBARMANDocumentsCodeszubis-martrou
tesusers.js:24:42)
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> (C:UsersBARMANDocumentsCodeszubis-martser
ver.js:15:15)
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)


Here's where I think the problem is from ##



const express = require('express');
const bcrypt = require('bcryptjs');
const fs = require('fs');
const mongoose = require('mongoose');
const multer = require('multer');
const passport = require('passport');

const config = require('../config/database');

mongoose.connect(config.database,
useNewUrlParser: true
);

let conn = mongoose.connection;

conn.once('open', () =>
console.log('Database File Upload Connection Established Successfully.');
);

conn.on('error', (err) =>
console.log('File Connection Error... ' + err);
);

const gridfs = require('mongoose-gridfs')(
collection: 'images',
model: 'Image',
mongooseConnection: conn
);


My Connection string if okay but I just can't seem to figure it out. Really need help please. Thanks Guys!










share|improve this question







New contributor




Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    2
    down vote

    favorite












    So I'm trying to use the mongoose-gridfs module with express but I get the following error when I try to run my express server.



    Error: Mongoose is not connected 
    at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
    _modulesmongoose-gridfslibstorage.js:43:11)
    at module.exports (C:UsersBARMANDocumentsCodeszubis-martnode_mo
    dulesmongoose-gridfslibstorage.js:472:18)
    at Object.<anonymous> (C:UsersBARMANDocumentsCodeszubis-martrou
    tesusers.js:24:42)
    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> (C:UsersBARMANDocumentsCodeszubis-martser
    ver.js:15:15)
    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)


    Here's where I think the problem is from ##



    const express = require('express');
    const bcrypt = require('bcryptjs');
    const fs = require('fs');
    const mongoose = require('mongoose');
    const multer = require('multer');
    const passport = require('passport');

    const config = require('../config/database');

    mongoose.connect(config.database,
    useNewUrlParser: true
    );

    let conn = mongoose.connection;

    conn.once('open', () =>
    console.log('Database File Upload Connection Established Successfully.');
    );

    conn.on('error', (err) =>
    console.log('File Connection Error... ' + err);
    );

    const gridfs = require('mongoose-gridfs')(
    collection: 'images',
    model: 'Image',
    mongooseConnection: conn
    );


    My Connection string if okay but I just can't seem to figure it out. Really need help please. Thanks Guys!










    share|improve this question







    New contributor




    Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      So I'm trying to use the mongoose-gridfs module with express but I get the following error when I try to run my express server.



      Error: Mongoose is not connected 
      at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
      _modulesmongoose-gridfslibstorage.js:43:11)
      at module.exports (C:UsersBARMANDocumentsCodeszubis-martnode_mo
      dulesmongoose-gridfslibstorage.js:472:18)
      at Object.<anonymous> (C:UsersBARMANDocumentsCodeszubis-martrou
      tesusers.js:24:42)
      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> (C:UsersBARMANDocumentsCodeszubis-martser
      ver.js:15:15)
      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)


      Here's where I think the problem is from ##



      const express = require('express');
      const bcrypt = require('bcryptjs');
      const fs = require('fs');
      const mongoose = require('mongoose');
      const multer = require('multer');
      const passport = require('passport');

      const config = require('../config/database');

      mongoose.connect(config.database,
      useNewUrlParser: true
      );

      let conn = mongoose.connection;

      conn.once('open', () =>
      console.log('Database File Upload Connection Established Successfully.');
      );

      conn.on('error', (err) =>
      console.log('File Connection Error... ' + err);
      );

      const gridfs = require('mongoose-gridfs')(
      collection: 'images',
      model: 'Image',
      mongooseConnection: conn
      );


      My Connection string if okay but I just can't seem to figure it out. Really need help please. Thanks Guys!










      share|improve this question







      New contributor




      Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      So I'm trying to use the mongoose-gridfs module with express but I get the following error when I try to run my express server.



      Error: Mongoose is not connected 
      at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
      _modulesmongoose-gridfslibstorage.js:43:11)
      at module.exports (C:UsersBARMANDocumentsCodeszubis-martnode_mo
      dulesmongoose-gridfslibstorage.js:472:18)
      at Object.<anonymous> (C:UsersBARMANDocumentsCodeszubis-martrou
      tesusers.js:24:42)
      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> (C:UsersBARMANDocumentsCodeszubis-martser
      ver.js:15:15)
      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)


      Here's where I think the problem is from ##



      const express = require('express');
      const bcrypt = require('bcryptjs');
      const fs = require('fs');
      const mongoose = require('mongoose');
      const multer = require('multer');
      const passport = require('passport');

      const config = require('../config/database');

      mongoose.connect(config.database,
      useNewUrlParser: true
      );

      let conn = mongoose.connection;

      conn.once('open', () =>
      console.log('Database File Upload Connection Established Successfully.');
      );

      conn.on('error', (err) =>
      console.log('File Connection Error... ' + err);
      );

      const gridfs = require('mongoose-gridfs')(
      collection: 'images',
      model: 'Image',
      mongooseConnection: conn
      );


      My Connection string if okay but I just can't seem to figure it out. Really need help please. Thanks Guys!







      javascript node.js mongodb mongoose






      share|improve this question







      New contributor




      Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 10 at 12:00









      Barman

      133




      133




      New contributor




      Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Barman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You should make sure the connection is opened first then initialize gridfs,



          conn.once('open', () => 
          global.gridfs = mongooseGridFS(
          collection: 'images',
          model: 'Image',
          mongooseConnection: conn
          );
          );





          share|improve this answer




















          • Thanks it works. But now I'm unable to access the gridfs variable from another block. So how do I access a variable defined with the "global" keyword?
            – Barman
            2 days ago

















          up vote
          0
          down vote













          As You may see from trace issue happens when it tries to create GridFSStorage instance and throws error because mongoose is not ready:




          Error: Mongoose is not connected

          at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
          _modulesmongoose-gridfslibstorage.js:43:11)







          Make sure mongoose connected to db before attaching gridfs.



          Here is fix:



          const express = require('express');
          const bcrypt = require('bcryptjs');
          const fs = require('fs');
          const mongoose = require('mongoose');
          const mongooseGridFS = require('mongoose-gridfs');
          const multer = require('multer');
          const passport = require('passport');


          const config = require('../config/database');

          mongoose.connect(config.database,
          useNewUrlParser: true
          );

          let conn = mongoose.connection;

          conn.once('open', () =>
          console.log('Database File Upload Connection Established Successfully.');

          global.gridfs = mongooseGridFS(
          collection: 'images',
          model: 'Image',
          mongooseConnection: conn
          );
          );

          conn.on('error', (err) =>
          console.log('File Connection Error... ' + err);
          );





          P.S. I've read mongoose-gridfs manual which insantiated exactly as in Your question which is incorrect.

          Since in code source at line #43 it immediately checks for connection state - which does tells that documentation is not correct.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Barman is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238727%2fthrow-new-errormongoose-is-not-connected%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You should make sure the connection is opened first then initialize gridfs,



            conn.once('open', () => 
            global.gridfs = mongooseGridFS(
            collection: 'images',
            model: 'Image',
            mongooseConnection: conn
            );
            );





            share|improve this answer




















            • Thanks it works. But now I'm unable to access the gridfs variable from another block. So how do I access a variable defined with the "global" keyword?
              – Barman
              2 days ago














            up vote
            0
            down vote



            accepted










            You should make sure the connection is opened first then initialize gridfs,



            conn.once('open', () => 
            global.gridfs = mongooseGridFS(
            collection: 'images',
            model: 'Image',
            mongooseConnection: conn
            );
            );





            share|improve this answer




















            • Thanks it works. But now I'm unable to access the gridfs variable from another block. So how do I access a variable defined with the "global" keyword?
              – Barman
              2 days ago












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            You should make sure the connection is opened first then initialize gridfs,



            conn.once('open', () => 
            global.gridfs = mongooseGridFS(
            collection: 'images',
            model: 'Image',
            mongooseConnection: conn
            );
            );





            share|improve this answer












            You should make sure the connection is opened first then initialize gridfs,



            conn.once('open', () => 
            global.gridfs = mongooseGridFS(
            collection: 'images',
            model: 'Image',
            mongooseConnection: conn
            );
            );






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 12:11









            Sletheren

            1,495315




            1,495315











            • Thanks it works. But now I'm unable to access the gridfs variable from another block. So how do I access a variable defined with the "global" keyword?
              – Barman
              2 days ago
















            • Thanks it works. But now I'm unable to access the gridfs variable from another block. So how do I access a variable defined with the "global" keyword?
              – Barman
              2 days ago















            Thanks it works. But now I'm unable to access the gridfs variable from another block. So how do I access a variable defined with the "global" keyword?
            – Barman
            2 days ago




            Thanks it works. But now I'm unable to access the gridfs variable from another block. So how do I access a variable defined with the "global" keyword?
            – Barman
            2 days ago












            up vote
            0
            down vote













            As You may see from trace issue happens when it tries to create GridFSStorage instance and throws error because mongoose is not ready:




            Error: Mongoose is not connected

            at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
            _modulesmongoose-gridfslibstorage.js:43:11)







            Make sure mongoose connected to db before attaching gridfs.



            Here is fix:



            const express = require('express');
            const bcrypt = require('bcryptjs');
            const fs = require('fs');
            const mongoose = require('mongoose');
            const mongooseGridFS = require('mongoose-gridfs');
            const multer = require('multer');
            const passport = require('passport');


            const config = require('../config/database');

            mongoose.connect(config.database,
            useNewUrlParser: true
            );

            let conn = mongoose.connection;

            conn.once('open', () =>
            console.log('Database File Upload Connection Established Successfully.');

            global.gridfs = mongooseGridFS(
            collection: 'images',
            model: 'Image',
            mongooseConnection: conn
            );
            );

            conn.on('error', (err) =>
            console.log('File Connection Error... ' + err);
            );





            P.S. I've read mongoose-gridfs manual which insantiated exactly as in Your question which is incorrect.

            Since in code source at line #43 it immediately checks for connection state - which does tells that documentation is not correct.






            share|improve this answer


























              up vote
              0
              down vote













              As You may see from trace issue happens when it tries to create GridFSStorage instance and throws error because mongoose is not ready:




              Error: Mongoose is not connected

              at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
              _modulesmongoose-gridfslibstorage.js:43:11)







              Make sure mongoose connected to db before attaching gridfs.



              Here is fix:



              const express = require('express');
              const bcrypt = require('bcryptjs');
              const fs = require('fs');
              const mongoose = require('mongoose');
              const mongooseGridFS = require('mongoose-gridfs');
              const multer = require('multer');
              const passport = require('passport');


              const config = require('../config/database');

              mongoose.connect(config.database,
              useNewUrlParser: true
              );

              let conn = mongoose.connection;

              conn.once('open', () =>
              console.log('Database File Upload Connection Established Successfully.');

              global.gridfs = mongooseGridFS(
              collection: 'images',
              model: 'Image',
              mongooseConnection: conn
              );
              );

              conn.on('error', (err) =>
              console.log('File Connection Error... ' + err);
              );





              P.S. I've read mongoose-gridfs manual which insantiated exactly as in Your question which is incorrect.

              Since in code source at line #43 it immediately checks for connection state - which does tells that documentation is not correct.






              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                As You may see from trace issue happens when it tries to create GridFSStorage instance and throws error because mongoose is not ready:




                Error: Mongoose is not connected

                at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
                _modulesmongoose-gridfslibstorage.js:43:11)







                Make sure mongoose connected to db before attaching gridfs.



                Here is fix:



                const express = require('express');
                const bcrypt = require('bcryptjs');
                const fs = require('fs');
                const mongoose = require('mongoose');
                const mongooseGridFS = require('mongoose-gridfs');
                const multer = require('multer');
                const passport = require('passport');


                const config = require('../config/database');

                mongoose.connect(config.database,
                useNewUrlParser: true
                );

                let conn = mongoose.connection;

                conn.once('open', () =>
                console.log('Database File Upload Connection Established Successfully.');

                global.gridfs = mongooseGridFS(
                collection: 'images',
                model: 'Image',
                mongooseConnection: conn
                );
                );

                conn.on('error', (err) =>
                console.log('File Connection Error... ' + err);
                );





                P.S. I've read mongoose-gridfs manual which insantiated exactly as in Your question which is incorrect.

                Since in code source at line #43 it immediately checks for connection state - which does tells that documentation is not correct.






                share|improve this answer














                As You may see from trace issue happens when it tries to create GridFSStorage instance and throws error because mongoose is not ready:




                Error: Mongoose is not connected

                at new GridFSStorage (C:UsersBARMANDocumentsCodeszubis-martnode
                _modulesmongoose-gridfslibstorage.js:43:11)







                Make sure mongoose connected to db before attaching gridfs.



                Here is fix:



                const express = require('express');
                const bcrypt = require('bcryptjs');
                const fs = require('fs');
                const mongoose = require('mongoose');
                const mongooseGridFS = require('mongoose-gridfs');
                const multer = require('multer');
                const passport = require('passport');


                const config = require('../config/database');

                mongoose.connect(config.database,
                useNewUrlParser: true
                );

                let conn = mongoose.connection;

                conn.once('open', () =>
                console.log('Database File Upload Connection Established Successfully.');

                global.gridfs = mongooseGridFS(
                collection: 'images',
                model: 'Image',
                mongooseConnection: conn
                );
                );

                conn.on('error', (err) =>
                console.log('File Connection Error... ' + err);
                );





                P.S. I've read mongoose-gridfs manual which insantiated exactly as in Your question which is incorrect.

                Since in code source at line #43 it immediately checks for connection state - which does tells that documentation is not correct.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 12:28

























                answered Nov 10 at 12:08









                num8er

                10.8k21738




                10.8k21738




















                    Barman is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Barman is a new contributor. Be nice, and check out our Code of Conduct.












                    Barman is a new contributor. Be nice, and check out our Code of Conduct.











                    Barman is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238727%2fthrow-new-errormongoose-is-not-connected%23new-answer', 'question_page');

                    );

                    Post as a guest














































































                    這個網誌中的熱門文章

                    What does pagestruct do in Eviews?

                    Dutch intervention in Lombok and Karangasem

                    Channel Islands