如何使用Mongoose连接MongoDB和Nodejs

nwlqm0z1  于 2023-02-08  发布在  Go
关注(0)|答案(1)|浏览(175)

我在YouTube上看了一个两年前的教程,我一直在这里。

throw error;
    ^

SetOptionError: mongodb+srv://BilalAhmed:password/Test: "mongodb+srv://BilalAhmed:password/Test" is not a valid option to set
    at Mongoose.set (F:\Webdevelopment\Node JS\node_modules\mongoose\lib\index.js:249:17)
    at Object.<anonymous> (F:\Webdevelopment\Node JS\NodeWithMongoDB.js:5:10)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  errors: {
    'mongodb+srv://BilalAhmed:password/Test': SetOptionInnerError [MongooseError]: "mongodb+srv://BilalAhmed:password/Test" is not a valid option to set     
        at Mongoose.set (F:\Webdevelopment\Node JS\node_modules\mongoose\lib\index.js:251:33)
        at Object.<anonymous> (F:\Webdevelopment\Node JS\NodeWithMongoDB.js:5:10)
        at Module._compile (node:internal/modules/cjs/loader:1159:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
        at Module.load (node:internal/modules/cjs/loader:1037:32)
        at Module._load (node:internal/modules/cjs/loader:878:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
        at node:internal/main/run_main_module:23:47
  }
}

const mongoose = require('mongoose')

mongoose.set('mongodb+srv://BilalAhmed:password/Test',
    {
        useNewUrlParser: false,
        useUnifiedTopology: true
    }).then(() => { console.warn("Mongodb connected")   })

我只想连接Mongo DB服务器并执行CRUD操作。

yuvru6vn

yuvru6vn1#

Const mongoose = require(“mongoose”)
mongoose.connect(‘mongodb://127.0.0.1:27017/databaseName, (error) =>{
if(error){
console.log(error);
} else{
console.log(‘Successfully Connected to mongodb’)
}
});

相关问题