lundi 21 octobre 2019

Proper Way to Connect MongoDB Native Driver Node JS

maybe the title and the question is a little dumb but I've been confused this all the time.

Here I have a file called service.js which will later be used to hold the Db instance

module.exports = {
  db: undefined
}

Mongo.js to connect and assign the db properties.

const { MongoClient } = require('mongodb')
const { mongoUrl, mongoOptions } = require('../../config')
const services = require('../services')

class Mongo {
  constructor () {
    this.client = new MongoClient(mongoUrl, mongoOptions)
  }

  init () {
    this.client.connect((err, res) => {
      if (!err) services.db = res.db()
    })
  }
}

module.exports = new Mongo()

And app.js to config the express and initiate mongo connection

const express = require('express')
const mongo = require('../lib/db/mongo')
const services = require('../lib/services')
const app = express()

mongo.init()

app.get('/', async (req, res) => {
  const User = services.db.collection('users')
  const getall = await User.find({}).sort({ _id: -1 }).limit(10).toArray()
  res.json(getall)
})

module.exports = app

Those code are able to run when I try to listen and access it. But the problem happens when I try to test it using Mocha

const app = require('../bin/app')
const chai = require('chai')
const expect = chai.expect
const request = require('supertest')

describe('Get User', () => {
  describe('GET /', () => {
    it('will get last 10 user', () => {
      request(app)
        .get('/')
        .expect('Content-Type', /json/)
        .expect(200)
        .end((end, res) => {
          console.log(res)
        })
    })
  })
})

I got error

(node:16652) UnhandledPromiseRejectionWarning: TypeError: services.db.collection is not a function
    at app.get (C:\Users\Lenovo\dev\absensi-selfie-api-new\bin\app.js:23:28)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:275:10)
    at urlencodedParser (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\body-parser\lib\types\urlencoded.js:91:7)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:275:10)
    at jsonParser (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\body-parser\lib\types\json.js:110:7)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:275:10)
    at cors (C:\Users\Lenovo\dev\absensi-selfie-api-new\bin\app.js:10:3)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:275:10)
    at logger (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\morgan\index.js:144:5)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:275:10)
    at expressInit (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:275:10)
    at query (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\middleware\query.js:45:5)
    at Layer.handle [as handle_request] (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\router\index.js:174:3)
    at Function.handle (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\application.js:174:10)
    at Server.app (C:\Users\Lenovo\dev\absensi-selfie-api-new\node_modules\express\lib\express.js:39:9)
    at Server.emit (events.js:189:13)
    at parserOnIncoming (_http_server.js:676:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
(node:16652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was
not handled with .catch(). (rejection id: 1)
(node:16652) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Maybe this could be done by using Mongoose, but I want to try it first using MongoDB native driver.

Any ideas? Thank you.

Aucun commentaire:

Enregistrer un commentaire