首页 > 解决方案 > 猫鼬 .save() 和 .find() 从不返回响应

问题描述

我正在尝试构建一个非常简单的快速应用程序。我需要能够连接到猫鼬并使用我定义的模式保存数据。我似乎一切都井井有条,但 logs.js 中的 .find() 和 .save() 调用从不返回响应。

这是 index.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}`);
});

这是logs.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.

等待解释。

标签: mongodbexpressmongoose

解决方案


推荐阅读