使用Node.js加载get请求时,我收到MongoClient连接错误响应
每当我试图通过Postman上的GET请求获取数据时,我都会遇到MongoDB Atlas连接错误,这在POST请求中并不总是发生。我刚开始这段旅程。非常感谢你的帮助使用Node.js加载get请求时,我收到MongoClient连接错误响应,node.js,mongodb,express,mongoose,mongodb-atlas,Node.js,Mongodb,Express,Mongoose,Mongodb Atlas,每当我试图通过Postman上的GET请求获取数据时,我都会遇到MongoDB Atlas连接错误,这在POST请求中并不总是发生。我刚开始这段旅程。非常感谢你的帮助 const express = require('express'); const mongoose = require('mongoose'); const router = express.Router(); const Product = require('../model/product'); router.get('
const express = require('express');
const mongoose = require('mongoose');
const router = express.Router();
const Product = require('../model/product');
router.get('/', (req, res, next) => {
Product.find()
.exec()
.then(docs=>{
console.log(docs);
res.status(200).json(docs);
})
.catch(err=>{
console.log(err);
res.status(500).json({error:err});
});
});
router.post('/', (req, res, next) => {
const product = new Product({
_id: mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product.save().then(result=>{
console.log(result);
})
.catch(err=> console.log(err));
res.status(200).json({
message:'Products Added!!! ',
createdProduct: product
});
});
我总是会遇到这样的错误:
Could not get any response
There was an error connecting to.
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Proxy configured incorrectly
Ensure that proxy is configured correctly in Settings > Proxy
Request timeout:
Change request timeout in Settings > General
如果你查看文档 猫鼬查询不是承诺。而是使用异步/等待
router.get("/", async (req,res)=>{
const docs=await Product.find().sort("name")
res.status(200).send(docs)
})
谢谢你,伊尔玛兹。我不得不从Atlas切换到本地的Mongodb.exe,一切都很顺利,先生。我真的很感谢你