0

I am trying to create a mean app. I am receiving errors while connecting mean app with MongoDb using mongoose. I am using "express": "^4.17.1", and "mongoose": "^6.0.7".

const mongoose = require('mongoose');

// Connecting to MongoDb
mongoose.connect(
  'mongodb+srv://MongoDb:<password>@cluster0.xfdie.mongodb.net/myFirstDatabase?retryWrites=true&w=majority',
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }
);

// On Connection
mongoose.connection.on('connected', () => {
  console.log('Connected to MongoDb'); 
});
mongoose.connection.on('error', (err) => {
  if (err) {
    console.log('Error in MongoDb Connection' + err); 
  }
});

It's working fine with the localhost but I am receiving the following error in Stackblitz.

Error in MongoDb ConnectionTypeError: this._handle[e] is not a function
(node:13) UnhandledPromiseRejectionWarning: TypeError: this._handle[e] is not a function
    at Resolver.querySrv [as resolveSrv] (https://file-sharing-portal.jw.staticblitz.com/blitz.fa2fa735bbb029186c23972eb56445ed5c5fdef5.js:6:585119)
    at Object.resolveSRVRecord (/home/projects/file-sharing-portal/node_modules/mongodb/lib/connection_string.js:55:9)
    at Object.connect (/home/projects/file-sharing-portal/node_modules/mongodb/lib/operations/connect.js:41:36)
    at eval (/home/projects/file-sharing-portal/node_modules/mongodb/lib/mongo_client.js:127:23)
    at Object.maybePromise (/home/projects/file-sharing-portal/node_modules/mongodb/lib/utils.js:518:5)
    at MongoClient.connect (/home/projects/file-sharing-portal/node_modules/mongodb/lib/mongo_client.js:126:24)
    at eval (/home/projects/file-sharing-portal/node_modules/mongoose/lib/connection.js:785:12)
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/projects/file-sharing-portal/node_modules/mongoose/lib/connection.js:776:19)
    at eval (/home/projects/file-sharing-portal/node_modules/mongoose/lib/index.js:328:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Please assist.

Arun Saini
  • 11
  • 2

2 Answers2

1

I asked for help in stackblitz-help channel in discord for the same and was informed that Mongoose isn't supported yet : https://github.com/stackblitz/core/issues/251

Arun Saini
  • 11
  • 2
0
const mongoose = require('mongoose');
const app = express();


mongoose.Promise = global.Promise;
mongoose.connect("CONNECTION_STRING", "OPTIONS", (error) => {
   if (error)
       console.log("Connection error - ", error);
   else
       console.log("MongoDB connection established");
});

let server = http.createServer(app);
server.setTimeout(500000);
server.listen(3000);

try with above code snippets

Sufail Kalathil
  • 558
  • 6
  • 21
  • Thank you Sufail for your respose. I have tried the mentioned snippet, still its prompting for error. Please review https://stackblitz.com/edit/sample-server – Arun Saini Sep 22 '21 at 04:16