我遇到了这个问题,但找不到任何东西,我相信这与我的连接字符串有关吗?一些同学遇到了这个问题,但是他们使用的连接字符串对我不起作用。无法弄清楚这一点。有想法吗?
SERVER.JS:
var express = require("express");
var session = require('express-session');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
const axios = require('axios');
var app = express();
mongoose.Promise = global.Promise
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs')
mongoose.connect('mongodb://localhost/fuckface')
var QuoteSchema = new mongoose.Schema({
name: { type: String, required: true, minlength: 10},
quote: { type: String, required: true, maxlength: 30 },
}, {timestamps: true });
mongoose.model('Quote', QuoteSchema);
var Quote = mongoose.model('Quote');
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static(__dirname + "/static"))
app.use(session({
secret: 'kingmonkey',
resave: false,
saveUninitialized: true,
cookie: { maxAge: 60000 }
}))
app.get('/showform', function(request, response) {
response.render('showform')
})
app.get('/showquotes', function(request, response) {
Quote.find({}, function(err, data) {
if (err) {
console.log('You need a different career')
} else {
response.render('showquotes', {quotes: data})
}
})
})
app.post('/processquote', function(request, response) {
var quoteInstance = new Quote()
console.log(request.body, '<<< QUOTE INSTANCE')
quoteInstance.name = request.body.name
quoteInstance.quote = request.body.quote
console.log(quoteInstance.name, quoteInstance.quote)
quoteInstance.save(function(err){
console.log('Oopsie dasies, it didnt save')
})
response.redirect('showquotes')
})
app.listen(8000, function() {
console.log('king me 8000')
})
SHOWFORM.EJS:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="quotes" action="/processquote" method="post">
Name: <input type="text" name="name" value="">
Quote: <textarea placeholder="Enter your quote" name="quote" rows="8" cols="80"></textarea>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
快速跟踪:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
(node:30200) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
king me 8000
(node:30200) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND localhost localhost:27017]
{ name: 'er;ofijer;oigj',
quote: 'erlkfurnfoerfenrfih3riufhi',
submit: 'submit' } '<<< QUOTE INSTANCE'
er;ofijer;oigj erlkfurnfoerfenrfih3riufhi
MongoDB TRACE:
2018-07-09T16:32:40.473-0700 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2018-07-09T16:32:40.546-0700 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-07-09T16:32:40.552-0700 I NETWORK [initandlisten] waiting for connections on port 27017
答案 0 :(得分:0)
您的mongodb连接URL没有端口。试试这个
const dbUrl = 'mongodb://localhost:27017/dbName';
mongoose.connect(dbUrl);
mongoose.connection.on('connected', () => {
console.log(`mongoose connection open to ${dbUrl}`);
});
mongoose.connection.on('error', (err) => {
console.log(`mongoose connection err: `, err);
});