无法从node js应用程序连接到我的数据库



我一直试图将我的应用程序连接到我的数据库,但这是不可能的。我在下面试了一下,它仍然不能识别我的模型

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = process.env.MONGODB_URL;
// Database Name
const dbName = 'KaydeeAcedemy';
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});

我也试过使用这个连接字符串,但没有进展。

const { MongoClient } = require('mongodb');
const uri = process.env.MONGODB_URL;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});

当我尝试从我的应用程序中添加一个新用户时,我得到这个错误,因为数据库。

Operation `users.findOne()` buffering timed out after 10000ms

mongodb的url是这样的:

MONGODB_URL = mongodb+srv://Kaydeeacademy:**************@cluster0.ehclf.mongodb.net/KaydeeAcademy?retryWrites=true&w=majority

我不知道我是否在连接字符串或其他地方错过了什么。

你能测试/比较一下我的一些代码(工作)吗

// have you called in the dot.env file?
require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')

// don't include the "useNewUrlParser: true, useUnifiedTopology: true" see answer below:
// https://stackoverflow.com/questions/68915722/option-usefindandmodify-is-not-supported

// make sure there are no spaces in the dot.env file
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log("db connected!"))
.catch(err => console.error("db connection failed ", err))

相关内容

  • 没有找到相关文章

最新更新