0

I was minding my business until I stumble upon this TypeError. I tried my best to figure it out, but the codes won't work in my method.

const express = require("express");
const bodyParser = require("body-parser");
const mongoClient = require("mongodb").MongoClient;
const objectId = require("mongodb").ObjectID;
require("dotenv/config");

let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended:true }));
app.get('/', (req, res) => {

app.listen(4000, () => {
console.log("It's connected to the server!");
mongoClient.connect(process.env.DB_CONNECTION, { useNewUrlParser:true, useUnifiedTopology: true }, (client, err) => {
    if (err) {
        throw error
    };
    const dB = client.db(process.env.DB_NAME); //Not a function, why?
    dB.collection("cookie");
    console.log(`Connected to "${process.env.DB_NAME}" !`);
});

});

I hope I get some good advice on how to tackle the problem properly. :)

1 Answer 1

2

The callback for mongoClient.connect has the wrong order of arguments.

(client, err) => {...} should be (err, client) => {...}

Either it receives a real error or null, but any of them should not have a function called db on them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.