node.js - 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);
看截图:
解决方案
推荐阅读
- wordpress - 按角色获取用户表
- ios - Swift:AVAudioPlayer 没有播放,线程可能出错?
- java - Eclipse:“制作:*** 没有规则可以将目标设为‘全部’。停止。”
- templates - confd 中的 `getvs` 是否与 yaml 列表一起使用?
- c++ - C++ 中的括号重载运算符是否应该声明为 const 函数?
- php - 在 null 上调用成员函数 getAttribute() 时出错
- binary - 如何在 vhdl 的标准逻辑(无向量)中从整数转换为二进制
- c# - IdentityUser 的 IUserValidator
- java - 在android studio中使用三个不同的用户登录后打开三个不同的活动
- php - 如何从 PHP 中的字符串中删除多余的文本?