mongoose 我如何连接后端到mongo数据库..?[已关闭]

dnph8jn4  于 2023-02-16  发布在  Go
关注(0)|答案(2)|浏览(143)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
16小时前关门了。
Improve this question

const express= require("express");
const app = express();
const mongoose= require("mongoose");
const uri = "mongodb://localhost:27017/myapp";

const port = process.env.PORT || 3000;
 
app.use(express.json());
app.get("/", (req,res)=> {res.send("hello world")});

app.listen(3000, () => {console.log (`server is running on your ${port}`)});

mongoose.set('strictQuery', true);
mongoose.connect(uri, {
    useNewUrlParser:true ,
    useUnifiedTopology:true,
    useCreateIndex:true,
    useFindAndModify:false
}).then () => {console.log ("db connection done")
}).catch((e)=> {console.log ("no connnection");})

看到一个连接节点到mongodb的教程,但是它显示没有连接。我怎么才能连接它,如果不是本地的,那么如何与Map集。

lqfhib0f

lqfhib0f1#

让连接= mongoos.connect(URI)
常量端口=进程环境端口||三千;
应用程序侦听(3000,异步()=〉{连接控制台日志(server is running on your ${port})});

n3schb8v

n3schb8v2#

重新排列生产线顺序如下。它应该工作。

// first connect to db
mongoose.set('strictQuery', true);
mongoose.connect(uri, {
    useNewUrlParser:true ,
    useUnifiedTopology:true,
    useCreateIndex:true,
    useFindAndModify:false
}).then () => {
console.log ("db connection done")

// then start the express server
    app.listen(3000, () => {console.log (`server is running on your ${port}`)});

}).catch((e)=> {console.log ("no connnection");})

或者你也可以关注MongoDB官方网站link
希望能有所帮助!

相关问题