skip to Main Content

i am running express.js inside electron and i am trying to connect mongodb composs to expressjs

when i start my express.js using electron i am getting error in mongodb
i have created the db in mongodb composs here is the error

[![enter image description here](https://i.stack.imgur.com/k6mxb.png)](https://i.stack.imgur.com/k6mxb.png)

here is the code for express .js

const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");

async function startExpressServer() {
  // Connect to MongoDB
  await mongoose.connect("mongodb://localhost:27017/pos", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });
  console.log("Connected to MongoDB");

  const expressApp = express();
  expressApp.use(cors());

  expressApp.get("/", (req, res) => {
    res.send("Hello World from Express!");
  });

  return expressApp;
}

module.exports = { startExpressServer };


in main.js i have this code which stats my express app


app.on("ready", async () => {
  // Start the Express server
  const expressApp = await startExpressServer();
  const port = 3001;
  expressApp.listen(port, () => {
    console.log(`Server listening on port ${port}`);
  });

  createMainWindow();
});

this code worked and ran the express app before i added monogo db connetion

2

Answers


  1. To get rid of the first warning you can try this before the connect statement.

     mongoose.set('strictQuery', true);
    
    Login or Signup to reply.
  2. To suppress the warning make the mongoose.set('strictQuery', true);. and instead of localhost and 0.0.0.0

     mongoose.set('strictQuery', true);
     await mongoose.connect("mongodb://0.0.0.0:27017/pos", {
        useNewUrlParser: true, // no need this in the new version
        useUnifiedTopology: true, // no need this in the new version
      });
      console.log("Connected to MongoDB");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search