[英]I am unable to connect my MongoDb with Vs code
我的程序使用了 MongoDb Atlas,但是我无法连接到数据库,这里我使用 Postman 来执行不同的操作,但是每当我发送一些请求时,就会发生错误。
const mongoose = require('mongoose');
const Product = require('./modelss/product');
mongoose.connect('mongodb+srv://<UserName>:<Password>@cluster1.un9kapv.mongodb.net/?retryWrites=true&w=majority'
).then(() => {
console.log('Connected to the database!')
}).catch(() => {
console.log('Connection failed!')
});
const createProduct = async(req, res,next) => {
const createdProduct = new Product({
name: req.body.name,
price: req.body.price
});
console.log(createdProduct);
const result = await createProduct.save();
res.json(result);
};
const GetProduct = async(req,res,next) => {
const products = await Product.find().exec();
res.json(products);
}
exports.createProduct = createProduct;
exports.GetProduct = GetProduct;
在从 Postman 发送任何请求之前,它显示-:在此处输入图像描述
但发送请求后 - :在此处输入图像描述
要调试此类事情,您可以捕获错误,然后通过控制台将其记录下来-
catch(e){
console.log(e)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.