gpt4 book ai didi

javascript - 连接 MongoDB Native Driver Node JS 的正确方法

转载 作者:行者123 更新时间:2023-11-28 20:13:02 25 4
gpt4 key购买 nike

也许标题和问题有点愚蠢,但我一直对此感到困惑。

这里我有一个名为 service.js 的文件,稍后将用于保存 Db 实例

module.exports = {
db: undefined
}

用于连接和分配数据库属性的 ​​Mongo.js。

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()

和 app.js 配置 express 并启动 mongo 连接

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

当我尝试收听和访问这些代码时,它们能够运行。但是当我尝试使用 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)
})
})
})
})

我遇到了错误

(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.

也许这可以通过使用 Mongoose 来完成,但我想先使用 MongoDB 本地驱动程序进行尝试。

有什么想法吗?谢谢。

最佳答案

试试这个

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

mongo.init()
const services = require('../lib/services')
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

在mongodb连接初始化后导入服务

关于javascript - 连接 MongoDB Native Driver Node JS 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com