繁体   English   中英

MongDB 数据库不是在 Visual Studio 中创建的

[英]MongDB Database was not created in Visual Studio

未创建数据库

这是我的错误报告

state.db is not a function TypeError: state.db is not a function at Object.module.exports.get (C:\Users\sainupangad\Desktop\newproject\config\connection.js:28:18) at Object.addProduct (C:\Users\sainupangad\Desktop\newproject\helpers\product-helpers.js:8:12) 在 C:\Users\sainupangad\Desktop\newproject\routes\admin.js:51:17 在 Layer.handle [作为 handle_request] (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\layer.js:95:5) 在下一个 (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib \router\route.js:137:13) 在 Route.dispatch (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request ] (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\layer.js:95:5) 在 Z0D61F8370CAD1D412F80B 84D143E1257Z:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\index.js:281:22 在 Function.process_params (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\ index.js:335:12) 在下一个 (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\index.js:275:10)

这是我的代码

var MongoClient = require('mongodb').MongoClient;


const state = { db: null }


module.exports.connect = function (done) {
    const url = 'mongodb://localhost:27017'
    const dbname = 'shoping'


    MongoClient.connect(url, (err, data) => {
        if (err) return done(err)
        state.db = data.db(dbname)
        done()
    })
}

module.exports.get = function () {
    return state.db()
}

                                                                                                                                    

如果我们看看您导出的 function:

  module.exports.get = function () {
     return state.db()
  }

您正在调用 state.db 但正如我们在您的连接 function 中看到的那样,您已经构建了 db 实例,因此您不需要再次调用构造函数。

应该解决您的问题是将state.db()更改为state.db

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM