Я пытаюсь создать очень простое express приложение. Мне нужно иметь возможность подключаться к mon goose и сохранять данные, используя мою определенную схему. Кажется, у меня все в порядке, но вызовы .find () и .save () в журналах. js никогда не возвращает ответ.
Вот индекс. js
const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config({path: __dirname + '\\.env'});
const middleware = require('./middleware');
const logs = require('./api/logs');
mongoose.connect(process.env.DATABASE_URL /*DATABASE_URL=mongodb://localhost:2222/travel-log*/, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const app = express();
app.use(morgan('common'));
app.use(helmet());
const port = process.env.PORT || 2222;
app.use(cors({
origin: process.env.CORS_ORIGIN
}));
app.use(express.json());
app.use('/api/logs', logs);
app.use(middleware.notFound);
app.use(middleware.errorHandler);
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
Вот журналы. js
const { Router } = require('express');
const LogEntry = require('../models/logEntry');
const router = Router();
router.get('/', async (req, res, next) => {
try {
const entries = await LogEntry.find();
await res.json(entries);
} catch (error) {
next(error);
}
});
router.post('/', async (req, res, next) => {
try {
const logEntry = new LogEntry(req.body);
const createdEntry = await logEntry.save();
await res.json(createdEntry);
} catch (error) {
if (error.name === 'ValidationError') {
res.status(422);
}
next(error);
}
}
);
module.exports = router;
Любые объяснения, почему эти звонки не проходят? Важно отметить, что я получаю MongooseTimeoutError
постоянно через 30 секунд. Вот журналы:
(node:17704) UnhandledPromiseRejectionWarning: MongooseTimeoutError: Server selection timed out after 30000 ms
at new MongooseTimeoutError (C:\Users\jones\WebstormProjects\travel-log\node_modules\mongoose\lib\error\timeout.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\jones\WebstormProjects\travel-log\node_modules\mongoose\lib\connection.js:804:19)
at Mongoose.connect (C:\Users\jones\WebstormProjects\travel-log\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:\Users\jones\WebstormProjects\travel-log\server\src\index.js:12:10)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
(node:17704) 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:17704) [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.
Ожидаются объяснения.