i tried to fix this errors in my project based on express js i found that i can't find the error
here's my code
const mongodb = require("mongodb");
const dotenv = require("dotenv").config();
mongodb.connect(
process.env.CONNECTION,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (err, client) {
if (err) {
console.log(err);
} else {
module.exports = client;
const app = require("./app");
app.listen(process.env.PORT, function () {
console.log("connected");
});
}
}
);
and the error i'm getting is this one
Users/ismailtaibi/Documents/GitHub/worketplace-web/db.js:4 mongodb.connect( ^
TypeError: mongodb.connect is not a function at Object. (/Users/ismailtaibi/Documents/GitHub/worketplace-web/db.js:4:9) at Module._compile (internal/modules/cjs/loader.js:1072:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) at Module.load (internal/modules/cjs/loader.js:937:32) at Function.Module._load (internal/modules/cjs/loader.js:778:12) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47
You can try below connect way.
const { MongoClient } = require('mongodb');
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
const client = new MongoClient(uri);
try {
await client.connect();
await listDatabases(client);
} catch (e) {
console.error(e);
}
or
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/mymondb", function (err, db) {
if(err) throw err;
console.log('mongodb is running!');
db.close();
});
or using mongoose
const express = require('express')
const mongoose = require('mongoose')
const app = express()
mongoose.connect('mongodb://localhost/db1', {
useNewUrlParser: true, useUnifiedTopology: true
})
app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended:false }))
app.listen(process.env.PORT || 5000);