当我尝试运行此代码时,出现以下错误

Posted

技术标签:

【中文标题】当我尝试运行此代码时,出现以下错误【英文标题】:When i am trying to run this code i am getting the following error 【发布时间】:2019-01-26 19:53:28 【问题描述】:

我正在尝试通过此代码连接 mongodb 数据库,但在运行此代码时出现错误(代码底部的错误)。 错误之一是在第 7 行,通过添加解决了 useNewUrlParser: true 但仍然有更多错误。我正在使用 MongoDB 版本4.0.1。有谁知道如何解决这个错误。

const express = require('express');     
const mongoose = require('mongoose');  
const bodyParser = require('body-parser');   
const app = express();

app.use(express.static('public'));  
app.set('view engine','ejs');

mongoose.connect('mongod://localhost/students',  useNewUrlParser: true );  
var studentSchema = new mongoose.Schema(  
    name: String,  
    age:Number,  
    country:String  
);

var Student = mongoose.model("Student", studentSchema);  
var shashank = new Student(  
    name:"Shashank",  
    age:"21",  
    country:"India"  
);    

shashank.save((err,student) =>     
    if(err)     
        console.log('Something went wrong!');     
        
    else       
        console.log("You added: " + student);      
          
);      

app.listen(3000,() =>      
    console.log('Server Listening!');     
);

运行此代码时出错!

D:\HARDWORK\YelpCamp-Course>node app.js
Server Listening!
                                                                                               (node:14060) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
    at parseConnectionString (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb-core\lib\uri_parser.js:216:21)
    at connect (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\operations\mongo_client_ops.js:179:3)
    at connectOp (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\operations\mongo_client_ops.js:283:3)
    at executeOperation (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\utils.js:420:24)
    at MongoClient.connect (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\mongo_client.js:168:10)
    at Promise (D:\HARDWORK\YelpCamp-Course\node_modules\mongoose\lib\connection.js:499:12)
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (D:\HARDWORK\YelpCamp-Course\node_modules\mongoose\lib\connection.js:496:19)
    at Mongoose.connect (D:\HARDWORK\YelpCamp-Course\node_modules\mongoose\lib\index.js:229:15)
    at Object.<anonymous> (D:\HARDWORK\YelpCamp-Course\app.js:9:10)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
(node:14060) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)                                                                                                                (node:14060) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

【参考方案1】:
mongoose.connect('mongod://localhost/students',  useNewUrlParser: true ); 

它应该以 mongodb://... 开头,并且当您使用 useNewUrlParser: true 时,您应该始终在连接字符串 mongoose reference 中指定端口号,如下所示。

mongoose.connect('mongodb://localhost:27017/students',  useNewUrlParser: true );

这里的端口号27017是MongoDB数据库的默认端口。

【讨论】:

谢谢!很多错误都消失了,但还有一些错误,请看一下。 (node:6516) UnhandledPromiseRejectionWarning: #&lt;Object&gt; (node:6516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:6516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 等我看看。【参考方案2】:

你写的主机不正确,应该是。米

mongoose.connect('mongodb://localhost:27017/students',  useNewUrlParser: true );

【讨论】:

【参考方案3】:

在连接到moongose之前使用这两个设置对于5.7版本更干净和稳定

mongoose.set('useNewUrlParser', true);
mongoose.set('useUnifiedTopology', true);

【讨论】:

【参考方案4】:

我也遇到了同样的问题,我只是在终端中通过命令启动了 mongo

1) sudo service mongod start 2) 蒙哥

为我工作。

【讨论】:

以上是关于当我尝试运行此代码时,出现以下错误的主要内容,如果未能解决你的问题,请参考以下文章

离子服务抛出此错误代码请帮助

当我尝试安装 Prometheus helm chart 时,出现此错误

此查询在 Access 2010 中完美运行,但是当我尝试在代码中执行它时,在 INSERT INTO 语句中出现语法错误

当我尝试为 scikit-learn 模型拟合另外 1 个功能时,出现此错误“ValueError:找到样本数量不一致的输入变量”

YouTube-dl 已更新到最新版本。当我运行此代码时,会出现这个长长的奇怪错误

构建失败并出现错误错误代码 72 命令:xcrun