![](/img/trans.png)
[英]I cam use POST and GET, but why I can't use DELETE in MongoDB Atlas
[英]Why can't I connect to mongoDB atlas?
我是 MongoDB 的新手,我一生都在使用 MySQL。
我在 atlas 中创建了一个帐户,将 IP 设置为我的 IP 并创建了一个用户并保存了密码。
这是我的代码,为什么它不起作用?
应用程序.js
const express = require('express'); const bodyParser = require('body-parser'); const mongoPractice = require('./mongo'); const app = express(); app.use(bodyParser.json()); app.post('/products', mongoPractice.createProduct); app.get('/products'); app.listen(3000);
和 mongo.js:
const MongoClient = require("mongodb").MongoClient; const url = "mongodb+srv://idan:<85IwoSzeQssHMzLN>@cluster0.tpejv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"; const createProduct = async (req, res, next) => { const newProduct = { name: req.body.name, price: req.body.price, }; const client = new MongoClient(url); try { await client.connect(); const db = client.db(); const result = db.collection("products").insertOne(newProduct); } catch (error) { return res.json(error); } client.close(); res.json(newProduct); }; const getProducts = async (req, res, next) => {}; exports.createProduct = createProduct; exports.getProducts = getProducts;
POSTMAN output:
Your ip may have changed, (check if the current ip address has information "(includes your current IP address)"
. For testing(.) you can add address 0.0.0.0/0 to the whitelist - it means every ip will be accepted - 这个解决方案适合初学者
在 mongo.js
您需要删除密码周围的“< >”。
const url = "mongodb+srv://idan:**85IwoSzeQssHMzLN**@cluster0.tpejv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
我曾经遇到过同样的错误,我可能有解决方案。
最常见的是您设置为访问数据库的 IP 地址可能与您当前的 IP 地址不匹配,在这种情况下,您需要将其设置为您当前的 IP 或设置为allow access from anywhere.
我遇到的问题:如果您最近开始使用ethernet cable
,请尝试back to wireless
以从后端脚本访问 mongoDB 数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.