如何解决此错误?猫鼬连接错误

时间:2020-04-12 01:58:46

标签: node.js mongodb mongoose

我正在尝试运行我的应用程序,并且一直在给我这个错误,这是从Sitepoint的演示应用程序获得的。这是应用程序的详细信息 https://www.sitepoint.com/build-simple-beginner-app-node-bootstrap-mongodb/

错误:


     throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
        ^
    MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mo
    ngoose.connect()` or `mongoose.createConnection()` is a string.
        at new MongooseError (C:\Users\tychi\WebstormProjects\demo-node-app\node_modules\mongoose\lib\error\mongooseError.js:
    10:11)
        at NativeConnection.Connection.openUri (C:\Users\tychi\WebstormProjects\demo-node-app\node_modules\mongoose\lib\conne
    ction.js:579:11)
        at Mongoose.connect (C:\Users\tychi\WebstormProjects\demo-node-app\node_modules\mongoose\lib\index.js:333:15)
        at Object.<anonymous> (C:\Users\tychi\WebstormProjects\demo-node-app\start.js:4:10)
        at Module._compile (internal/modules/cjs/loader.js:688:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
        at Module.load (internal/modules/cjs/loader.js:598:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
        at Function.Module._load (internal/modules/cjs/loader.js:529:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
        at startup (internal/bootstrap/node.js:285:19)
        at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
    [nodemon] app crashed - waiting for file changes before starting...

代码:

require('dotenv').config();
const mongoose = require('mongoose');

mongoose.connect(process.env.demoapp ,
    { useNewUrlParser: true },
    { useUnifiedTopology: true })


mongoose.connection
    .on('open', () => {
        console.log('Mongoose connection open');
    })
    .on('error', (err) => {
        console.log(`Connection error: ${err.message}`);
    });

require('./models/Registration');

const app = require('./app');

const server = app.listen(3000, () => {
    console.log(`Express is running on port ${server.address().port}`);
});

0 个答案:

没有答案