mongodb 如何将mongo db组合连接到express.js

fhg3lkii  于 2023-03-01  发布在  Go
关注(0)|答案(2)|浏览(160)

我在electron内部运行express.js,并且尝试将mongodb composs连接到expressjs
当我使用electron启动express.js时,我在mongodb中得到错误,我已经在mongodb composs中创建了数据库,这里是错误

下面是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 };

在main.js中,我有这段代码,它可以统计我的Express应用程序

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

此代码工作,并运行express应用程序之前,我添加monogo db连接

sgtfey8w

sgtfey8w1#

要抑制警告,请使用mongoose.set('strictQuery', true); .和代替localhost0.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");
uoifb46i

uoifb46i2#

要摆脱第一个警告,您可以在connect语句之前尝试此操作。

mongoose.set('strictQuery', true);

相关问题