首页 > 解决方案 > MongoDB 失去智能感知?VSCode

问题描述

我正在学习 MongoDB,但我一直遇到一个奇怪的问题,我无法理解,这是我的代码:

// === MODULES
const routes = require('express').Router();
require('dotenv').config();

// === MONGODB
const { connectMongo } = require('../mongodb');

// === CONSTANTS
const mongoDB = process.env.MONGO_DB_NAME;

// === ROUTES
// GET: Returns a list of 'Clients' contained within MongoDB Client Collection
routes.get('/', async (req, res, next) => {
  try {
    // Acquire our Connection
    const connection = await connectMongo();
    // Select the Database
    // TODO: .env the MongoDB Database Name
    const db = await connection.db(mongoDB);
    // Find our Collection
    const collection = await db.collection('clients');
    // Grab our Clients and map to an array
    const clients = await collection.find({}).toArray();
    // Close our connection
    await connection.close();

    // Send the client data
    res.send(clients);
  } catch (err) {
    next(err);
  }
});

// POST: Adds a new Client to the Clients collection
routes.post('/add', async (req, res, next) => {
  try {
    // Client should be posted from a form, which is exposed on req.body through bodyParser
    const client = req.body;

    if (!client) {
      res.status(400).json({
        error: 'No client was specified',
      });
    }

    const connection = await connectMongo();
    const db = await connection.db(mongoDB);
    const collection = await db.

    res.send(client);
  } catch (err) {
    next(err);
  }
});

// === EXPORT
module.exports = routes;

在“GET”路线中,智能感知按我的预期工作,例如:

await connection.close();

我得到了 .close(); 的智能感知。

但是,在 POST 路由中,智能感知仅适用于:

const db = await connection.db(mongoDB);

看截图:

智能感知截图

标签: node.jsmongodbexpressvisual-studio-codeintellisense

解决方案


推荐阅读