homePage.create不工作。我想我的问题出在这里"mongoose.connect('mongo://localhost:27017/Socialuser', { useNewUrlParser: true, useUnifiedTopology: true });"
App.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‘将出现.this问题
(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块的异步函数内部引发,或拒绝未使用.catch()处理的promise造成的。要在未处理的promise rejection上终止节点进程,请使用CLI标志--unhandled-rejections=strict
(请参见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。(拒绝id: 3) (节点:22464) DEP0018 DeprecationWarning:未处理的promise拒绝已弃用。将来,未处理的promise拒绝将使用非零退出代码终止Node.js进程。
发布于 2020-08-05 08:40:35
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!
https://stackoverflow.com/questions/63261128
复制