简体   繁体   English

不要在节点 js 中连接到 mongodb

[英]do not conneting to mongodb in node js

while writing code for connecting mongodb through node in a project it shows error在编写代码以通过项目中的节点连接 mongodb 时显示错误

i used the below code for connecting to mongodb我使用以下代码连接到 mongodb

var mongocilent=require('mongodb').mongocilent
var state = {db:null}
module.exports.connect=function(done){
var url ='mongodb://localhost:27017'
var dbname = "shopping" 
mongocilent.connect(url,(err,data)=>{
    if(err)return done(err)
        state.db=data.db(dbname)
        done()
    
})
module.exports.get=function(){
return state.db

} }

but if i start the server it shows:但如果我启动服务器,它会显示:

npm start
> shopping-cart@0.0.0 start
> nodemon ./bin/www

[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./bin/www`
/home/shinoy/Desktop/binoy/shopping cart/config/connection.js:8
mongocilent.connect(url,(err,data)=>{

            ^

TypeError: Cannot read properties of undefined (reading 'connect')
at module.exports.connect (/home/shinoy/Desktop/binoy/shopping 
cart/config/connection.js:8:17)

at Object.<anonymous> (/home/shinoy/Desktop/binoy/shopping cart/app.js:10:4)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
at Module.load (node:internal/modules/cjs/loader:998:32)
at Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1022:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/shinoy/Desktop/binoy/shopping cart/bin/www:7:11)
at Module._compile (node:internal/modules/cjs/loader:1120:14)

Node.js v18.7.0
[nodemon] app crashed - waiting for file changes before starting...

im tried different solution from google but its not work for me我尝试了与谷歌不同的解决方案,但它对我不起作用

You spelled it wrong in the first line on requiring the MongoClient object from mongodb.您在要求 mongodb 的 MongoClient object 的第一行中拼写错误。 just replace the first line with this只需用这个替换第一行

const mongoclient = require("mongodb").MongoClient;

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

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