Reputation: 47
IMPORTANT EDIT :
There is nothing wrong with my code, the problem come from my connection, my network at work is blocking it I dont know why, I used my phone connection to figure it out. I'll look into it.
Original message :
I'm trying to make a NodeJS API using Express, Mongoose with a MongoDB Atlas database and my level with Javascript is like beginner / intermediate if you wonder.
I'm following this tutorial : https://youtu.be/vjf774RKrLc?t=2159 but he is using MLab instead of MongoAtlas and I tried to adapat my code to it.
So the passage I posted of the video is where I'm having troubles. His post is working but mine just does nothing and I get this error from PostMan "Could not get any response"
So when I try to do my post my article.save()
is not working, and I dont know where I'm making an error. So there is my different files if you can look into it pls, there is not too much code for the moment so I'm posting the entire file.
app.js :
const express = require('express');
const app = express();
const mongoose = require('mongoose');
require('dotenv/config');
//simplify us the use of json objects and encoded urls with special characters
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//Import routes
const articlesRoute = require('./routes/articles.js');
//url redirections
// link to .../articles will be dealt with articlesRoute
app.use('/articles', articlesRoute);
//Home page of the API
app.get('/', (req, res) => {
res.send('We are at home!')
});
//database connection
mongoose.connect(
process.env.DB_CONNECTION,
{ dbName: 'test', useNewUrlParser: true, useUnifiedTopology: true },
() => {
console.log("Connected to db");
}
);
//listening port
app.listen(3000);
articles.js :
const express = require('express');
const router = express.Router();
const Article = require('../models/Article');
router.get('/', (req, res) => {
res.send('we are on articles');
});
router.post('/', async (req, res) => {
const article = new Article(req.body);
try {
console.log("try");
console.log(article);
const savedArticle = await article.save();
console.log(savedArticle);
res.json(savedArticle);
} catch (err) {
console.log("error :" + err)
res.json({ message: err })
};
});
module.exports = router;
Article.js :
const mongoose = require('mongoose');
const ArticleSchema = mongoose.Schema({
reference: {
type: String,
required: true
},
categorie: {
type: String,
required: true
},
description: {
type: String,
required: false
},
garantie: {
type: Number,
required: true
}
});
module.exports = mongoose.model('Articles', ArticleSchema);
My 'article' object has the data but nothing happens, PostMan just load for some minutes and then I get the message posted before.
I also tried to the fixes adviced by PostMan but it changes nothing.
Thanks for the upcomings answers.
EDIT :
Thanks to @SuleymanSah I can see that I have an error when I connect to my database :
{ Error: connect ECONNREFUSED there.Is.An.Ip.There at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ], [Symbol(mongoErrorContextSymbol)]: {} }
But I dont understand why, If anyone has a solutions thx
Upvotes: 2
Views: 1569
Reputation: 39
Probably, you should try to check the network access in the mongo db atlas. And change the IP white list to allow from anywhere.
Upvotes: 0
Reputation: 17868
In dotenv docs, the require is like this:
require('dotenv').config();
Also can you connect mongoose like this, so that we can see if there is an error when connecting.
Can you try with these changes?
mongoose
.connect(process.env.DB_CONNECTION, { dbName: 'test', useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Connected to db"))
.catch(err => console.log(`Could not Connected to db ${process.env.DB_CONNECTION} `, err));
EDIT:
console.log(err) showed a network error, so after fixing it, connection succeed, and the app worked.
Upvotes: 2