2 years ago

#66970

test-img

GuelhaS

mongo database connection problem using mongodb library

I'm kinda new and i'm gettina an error when reusing an old node.js app to connect to mongo db database. Does anyone know whats the problem with this? It does show the console.log before the connecting but does not get past const [DB, ObjectID, DBClient] = await this.Mongodb.onConnect(); I'm using mongodb library, not mongoose

const Mongodb = require("../config/db");
class QueryController {
  constructor() {
    this.Mongodb = Mongodb;
  }
  login(data) {
    //console.log(data);
    return new Promise(async (resolve, reject) => {
      try {
        console.log("login antes de tentar ligacao à BD");
        const [DB, ObjectID, DBClient] = await this.Mongodb.onConnect();
        //console.log(DB.data);
        DB.collection("users").findOneAndUpdate(
          data,
          {
            $set: {
              online: "Y",
            },
          },
          (error, result) => {
            DBClient.close();
            if (error) {
              reject(error);
            }
            result.lastErrorObject.updatedExisting
              ? resolve(result.value._id)
              : resolve(null);
          }
        );
      } catch (error) {
        reject(error);
      }
    });
  }

Can anyone help me?

the ../config/db file looks like this

const mongodb = require("mongodb");

class MongoDB {
  constructor() {
    this.mongoClient = mongodb.MongoClient;
    this.ObjectID = mongodb.ObjectId;
  }

  onConnect() {
    console.log("Entrou no onConnect");
    return new Promise((resolve, reject) => {
      this.mongoClient.connect(
        process.env.MONGODB_DB_URL,
        {
          useNewUrlParser: true,
        },
        (err, client) => {
          if (err) {
            console.log("Não Entrou na BD");
            reject(err);
          } else {
            resolve([client.db("users"), this.ObjectID, client]);
          }
        }
      );
    });
  }
}
module.exports.MongoDB = new MongoDB();

at the end, when i console.log the error at the catch, it logs the following message

[nodemon] starting `node index.js`
Listening on http://localhost:4000
Entrou!
login antes de tentar ligacao à BD
TypeError: this.Mongodb.onConnect is not a function
    at D:\MEI\AIS\Trabalho-AIS\API-Users\controllers\query-controller.js:13:61
    at new Promise (<anonymous>)
    at QueryController.login (D:\MEI\AIS\Trabalho-AIS\API-Users\controllers\query-controller.js:10:12)
    at loginRouteController (D:\MEI\AIS\Trabalho-AIS\API-Users\controllers\route-controller.js:60:36)
    at Layer.handle [as handle_request] (D:\MEI\AIS\Trabalho-AIS\API-Users\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\MEI\AIS\Trabalho-AIS\API-Users\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\MEI\AIS\Trabalho-AIS\API-Users\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\MEI\AIS\Trabalho-AIS\API-Users\node_modules\express\lib\router\layer.js:95:5)
    at D:\MEI\AIS\Trabalho-AIS\API-Users\node_modules\express\lib\router\index.js:281:22  
    at Function.process_params (D:\MEI\AIS\Trabalho-AIS\API-Users\node_modules\express\lib\router\index.js:341:12)

node.js

mongodb

database-connection

node.js

mongodb

database-connection

0 Answers

Your Answer

Accepted video resources