簡體   English   中英

'UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string' 問題正在發生我不知道是什么問題

[英]'UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string' problem is going on i didn't know what is problem

homePage.create 不工作。 我認為我的問題在這里"mongoose.connect('mongo://localhost:27017/Socialuser', { useNewUrlParser: true, useUnifiedTopology: true });"

應用程序.js 文件

          const express = require('express');
            const bodyParser = require('body-parser');
            const mongoose = require('mongoose');
            let homePage = require('./models/home');
            
            mongoose.connect('mongo://localhost:27017/Socialuser', { useNewUrlParser: true, useUnifiedTopology: true });
            
            const app = express();
            
            app.use(express.static(__dirname + '/public'));
            
            app.get('/', (req, res) => {
                res.render('landing.ejs');
            });
            
            app.get('/login', (req, res) => {
                res.render('loginSignUp.ejs');
            });
            
            app.get('/home', (req, res) => {
            
            
            
                console.log(homePage);
                homePage.create({ name: 'wasim', image: 'https://www.w3schools.com/w3css/img_lights.jpg' }, (err, home) => {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log('saved');
                    }
                });
            });
            
            app.listen(3000, () => {
                console.log('server started at port 3000');
            });
    
   

這是我的 home.js 文件。這是主模式文件

    const mongoose = require('mongoose');
    
    let homePageSchema = new mongoose.Schema({
        name: String,
        image: String
        // comments: [ String ]
    });
    
    module.exports = mongoose.model('Home', homePageSchema);
    

什么是實際問題我沒有得到發生了什么。 'UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string' .....這個問題正在發生

    (node:22464) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
        at parseConnectionString (C:\Users\wasim\Desktop\social\node_modules\mongodb\lib\core\uri_parser.js:547:21)
        at connect (C:\Users\wasim\Desktop\social\node_modules\mongodb\lib\operations\connect.js:277:3)
        at C:\Users\wasim\Desktop\social\node_modules\mongodb\lib\mongo_client.js:222:5
        at maybePromise (C:\Users\wasim\Desktop\social\node_modules\mongodb\lib\utils.js:662:3)
        at MongoClient.connect (C:\Users\wasim\Desktop\social\node_modules\mongodb\lib\mongo_client.js:218:10)
        at C:\Users\wasim\Desktop\social\node_modules\mongoose\lib\connection.js:713:12
        at new Promise (<anonymous>)
        at NativeConnection.Connection.openUri (C:\Users\wasim\Desktop\social\node_modules\mongoose\lib\connection.js:710:19)
        at Mongoose.connect (C:\Users\wasim\Desktop\social\node_modules\mongoose\lib\index.js:335:15)
        at Object.<anonymous> (C:\Users\wasim\Desktop\social\app.js:6:10)
        at Module._compile (internal/modules/cjs/loader.js:1133:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
        at Module.load (internal/modules/cjs/loader.js:977:32)
        at Function.Module._load (internal/modules/cjs/loader.js:877:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
        at internal/main/run_main_module.js:18:47
    (node:22464) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 

此錯誤源於在沒有 catch 塊的情況下拋出異步 function 內部,或拒絕未使用.catch() 處理的 promise。 要終止未處理的 promise 拒絕的節點進程,請使用 CLI 標志--unhandled-rejections=strict (請參閱https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (拒絕 id:3)(節點:22464)[DEP0018] DeprecationWarning:不推薦使用未處理的 promise 拒絕。 將來,未處理的 promise 拒絕將使用非零退出代碼終止 Node.js 進程。

    1) Please use this 
    
    mongodb://localhost:27017/Socialuser
    
    Instead of -
    
    mongo://localhost:27017/Socialuser
    
    2) mongoose.connect("mongodb://localhost:27017/[yourDbName]", {
      useUnifiedTopology: true,
      useNewUrlParser: true
    });
    
    In your case yourDbName = Socialuser

Hope this will solve your problem!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM