链接 node db/db.js出错

链接 node db/db.js出错

F:\代码练习\代码练习\Nodejs\test4>node db/db.js
mongoose 连接出错 MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
    at new ConnectionString (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongodb-connection-string-url\lib\index.js:86:19)
    at parseOptions (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongodb\lib\connection_string.js:204:17)
    at new MongoClient (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongodb\lib\mongo_client.js:46:63)
    at _createMongoClient (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongoose\lib\connection.js:851:14)
    at NativeConnection.openUri (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongoose\lib\connection.js:705:29)
    at Mongoose.connect (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongoose\lib\index.js:406:15)
    at Object.<anonymous> (F:\代码练习\代码练习\Nodejs\test4\db\db.js:8:10)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
    at new ConnectionString (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongodb-connection-string-url\lib\index.js:86:19)
    at parseOptions (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongodb\lib\connection_string.js:204:17)
    at new MongoClient (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongodb\lib\mongo_client.js:46:63)
    at _createMongoClient (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongoose\lib\connection.js:851:14)
    at NativeConnection.openUri (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongoose\lib\connection.js:705:29)
    at Mongoose.connect (F:\代码练习\代码练习\Nodejs\test4\node_modules\mongoose\lib\index.js:406:15)
    at Object.<anonymous> (F:\代码练习\代码练习\Nodejs\test4\db\db.js:8:10)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)

F:\代码练习\代码练习\Nodejs\test4>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕久久 2023-03-13 13:07:26

同学你好,报错提示“无效的scheme”:

https://img1.sycdn.imooc.com//climg/640eaf2b09c8050806130087.jpg

怀疑代码书写有误。建议把自己写的那部分项目代码粘贴出来,老师需要测试才能定位到具体哪里有问题。

祝学习愉快!

  • 提问者 慕田峪1427181 #1
    //数据模型(规范数据格式)
    
    const mongoose = require('./db')
    //定义schema (数据规范)
    
    const UserSchema = mongoose.Schema({
        username:{
            type:String,
            required:true,
            unique:true
        },
        password:String,
        age:Number,
        city:String,
        //性别
        gender:{
            type:Number,
            default:0
        }
    },{
        timestamps:true //
    }
    )
    //定义model
    const User = mongoose.model('user',UserSchema)
    
    module.exports = {
        User
    }
    
    
    
    
    const mongoose = require('mongoose')
    
    const url = 'mongoodb://localhost:27017'
    const dbName = 'comment2'
    
    
    
    mongoose.connect(`${url}/${dbName}`,{
        useNewUrlParser:true,
        useUnifiedTopology:true
    })
    
    const conn = mongoose.connection
    
    conn.on('error',err =>{
        console.error('mongoose 连接出错',err);
    })
    
    module.exports = mongoose //commonjs


    2023-03-14 11:17:03
  • 好帮手慕久久 回复 提问者 慕田峪1427181 #2

    数据库的url地址写错了,所以报错了:

    https://img1.sycdn.imooc.com//climg/640fea550902fa9904910125.jpg

    修改如下:

    https://img1.sycdn.imooc.com//climg/640fea6509a142ad05480114.jpg

    同学改一下试试。

    2023-03-14 11:31:09
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师