Trying to post on mongoDB using mongoose (NODE.JS)
This is the model of the product, where i have the mongoose schema of the object.
const mongoose = require('mongoose');
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
price: Number
});
module.exports = mongoose.model('Product', productSchema);
This is my method POST.
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const Product = require("../models/product");
router.post("/", (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
The problem is the following: whenever i call the method POST on POSTMAN the server never respondes, it gives the error 500 on visual studio code and the server crashes. The body used in POSTMAN is correct and also the connection to the mongoDB is successful.
The server prints the following error:
(node:3214) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017 timed out]
(node:3214) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3214) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
node.js mongodb mongoose
add a comment |
This is the model of the product, where i have the mongoose schema of the object.
const mongoose = require('mongoose');
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
price: Number
});
module.exports = mongoose.model('Product', productSchema);
This is my method POST.
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const Product = require("../models/product");
router.post("/", (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
The problem is the following: whenever i call the method POST on POSTMAN the server never respondes, it gives the error 500 on visual studio code and the server crashes. The body used in POSTMAN is correct and also the connection to the mongoDB is successful.
The server prints the following error:
(node:3214) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017 timed out]
(node:3214) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3214) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
node.js mongodb mongoose
as a general advice, POSTMAN is usually not very reliable, you should really consider start using curl to test Webservices. If you are in Windows, you can use GitBash or LSW.
– Matias Barrios
Nov 23 '18 at 18:56
MongoNetworkError: failed to connect to server- The application cannot connect to your MongoDB server. This is a networking configuration problem and not related to the code in the question. The unhandled promise rejection calls are because you are callingmongoose.connect()without using a.catch()or alternately inside atry..catchblock with async/await. You should fix that code, but the real problem here is networking.
– Neil Lunn
Nov 24 '18 at 2:10
try to restart your mongodb service by: sudo service mongod start
– Raunik Singh
Nov 24 '18 at 15:52
add a comment |
This is the model of the product, where i have the mongoose schema of the object.
const mongoose = require('mongoose');
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
price: Number
});
module.exports = mongoose.model('Product', productSchema);
This is my method POST.
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const Product = require("../models/product");
router.post("/", (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
The problem is the following: whenever i call the method POST on POSTMAN the server never respondes, it gives the error 500 on visual studio code and the server crashes. The body used in POSTMAN is correct and also the connection to the mongoDB is successful.
The server prints the following error:
(node:3214) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017 timed out]
(node:3214) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3214) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
node.js mongodb mongoose
This is the model of the product, where i have the mongoose schema of the object.
const mongoose = require('mongoose');
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
price: Number
});
module.exports = mongoose.model('Product', productSchema);
This is my method POST.
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const Product = require("../models/product");
router.post("/", (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
The problem is the following: whenever i call the method POST on POSTMAN the server never respondes, it gives the error 500 on visual studio code and the server crashes. The body used in POSTMAN is correct and also the connection to the mongoDB is successful.
The server prints the following error:
(node:3214) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to node-rest-shop-shard-00-00-pbcph.azure.mongodb.net:27017 timed out]
(node:3214) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3214) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
node.js mongodb mongoose
node.js mongodb mongoose
edited Nov 23 '18 at 18:23
Luis Maia
asked Nov 23 '18 at 17:35
Luis MaiaLuis Maia
487
487
as a general advice, POSTMAN is usually not very reliable, you should really consider start using curl to test Webservices. If you are in Windows, you can use GitBash or LSW.
– Matias Barrios
Nov 23 '18 at 18:56
MongoNetworkError: failed to connect to server- The application cannot connect to your MongoDB server. This is a networking configuration problem and not related to the code in the question. The unhandled promise rejection calls are because you are callingmongoose.connect()without using a.catch()or alternately inside atry..catchblock with async/await. You should fix that code, but the real problem here is networking.
– Neil Lunn
Nov 24 '18 at 2:10
try to restart your mongodb service by: sudo service mongod start
– Raunik Singh
Nov 24 '18 at 15:52
add a comment |
as a general advice, POSTMAN is usually not very reliable, you should really consider start using curl to test Webservices. If you are in Windows, you can use GitBash or LSW.
– Matias Barrios
Nov 23 '18 at 18:56
MongoNetworkError: failed to connect to server- The application cannot connect to your MongoDB server. This is a networking configuration problem and not related to the code in the question. The unhandled promise rejection calls are because you are callingmongoose.connect()without using a.catch()or alternately inside atry..catchblock with async/await. You should fix that code, but the real problem here is networking.
– Neil Lunn
Nov 24 '18 at 2:10
try to restart your mongodb service by: sudo service mongod start
– Raunik Singh
Nov 24 '18 at 15:52
as a general advice, POSTMAN is usually not very reliable, you should really consider start using curl to test Webservices. If you are in Windows, you can use GitBash or LSW.
– Matias Barrios
Nov 23 '18 at 18:56
as a general advice, POSTMAN is usually not very reliable, you should really consider start using curl to test Webservices. If you are in Windows, you can use GitBash or LSW.
– Matias Barrios
Nov 23 '18 at 18:56
MongoNetworkError: failed to connect to server - The application cannot connect to your MongoDB server. This is a networking configuration problem and not related to the code in the question. The unhandled promise rejection calls are because you are calling mongoose.connect() without using a .catch() or alternately inside a try..catch block with async/await. You should fix that code, but the real problem here is networking.– Neil Lunn
Nov 24 '18 at 2:10
MongoNetworkError: failed to connect to server - The application cannot connect to your MongoDB server. This is a networking configuration problem and not related to the code in the question. The unhandled promise rejection calls are because you are calling mongoose.connect() without using a .catch() or alternately inside a try..catch block with async/await. You should fix that code, but the real problem here is networking.– Neil Lunn
Nov 24 '18 at 2:10
try to restart your mongodb service by: sudo service mongod start
– Raunik Singh
Nov 24 '18 at 15:52
try to restart your mongodb service by: sudo service mongod start
– Raunik Singh
Nov 24 '18 at 15:52
add a comment |
1 Answer
1
active
oldest
votes
i think your problem it may that yo don't exporting the router
var express = require('express');
var Product = require('../models/product');
const router = express.Router();
const mongoose = require('mongoose');
router.post('/product', (req, res) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
module.exports = router;
Try this code i test it in postman.
Post product
That's not the issue. There is an error message in the question which shows exactly what is wrong. Nothing to do with the code in the question.
– Neil Lunn
Nov 24 '18 at 2:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450924%2ftrying-to-post-on-mongodb-using-mongoose-node-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
i think your problem it may that yo don't exporting the router
var express = require('express');
var Product = require('../models/product');
const router = express.Router();
const mongoose = require('mongoose');
router.post('/product', (req, res) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
module.exports = router;
Try this code i test it in postman.
Post product
That's not the issue. There is an error message in the question which shows exactly what is wrong. Nothing to do with the code in the question.
– Neil Lunn
Nov 24 '18 at 2:12
add a comment |
i think your problem it may that yo don't exporting the router
var express = require('express');
var Product = require('../models/product');
const router = express.Router();
const mongoose = require('mongoose');
router.post('/product', (req, res) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
module.exports = router;
Try this code i test it in postman.
Post product
That's not the issue. There is an error message in the question which shows exactly what is wrong. Nothing to do with the code in the question.
– Neil Lunn
Nov 24 '18 at 2:12
add a comment |
i think your problem it may that yo don't exporting the router
var express = require('express');
var Product = require('../models/product');
const router = express.Router();
const mongoose = require('mongoose');
router.post('/product', (req, res) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
module.exports = router;
Try this code i test it in postman.
Post product
i think your problem it may that yo don't exporting the router
var express = require('express');
var Product = require('../models/product');
const router = express.Router();
const mongoose = require('mongoose');
router.post('/product', (req, res) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Handling POST requests to /products",
createdProduct: result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
module.exports = router;
Try this code i test it in postman.
Post product
answered Nov 23 '18 at 19:48
Leonardo BLeonardo B
282
282
That's not the issue. There is an error message in the question which shows exactly what is wrong. Nothing to do with the code in the question.
– Neil Lunn
Nov 24 '18 at 2:12
add a comment |
That's not the issue. There is an error message in the question which shows exactly what is wrong. Nothing to do with the code in the question.
– Neil Lunn
Nov 24 '18 at 2:12
That's not the issue. There is an error message in the question which shows exactly what is wrong. Nothing to do with the code in the question.
– Neil Lunn
Nov 24 '18 at 2:12
That's not the issue. There is an error message in the question which shows exactly what is wrong. Nothing to do with the code in the question.
– Neil Lunn
Nov 24 '18 at 2:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450924%2ftrying-to-post-on-mongodb-using-mongoose-node-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
as a general advice, POSTMAN is usually not very reliable, you should really consider start using curl to test Webservices. If you are in Windows, you can use GitBash or LSW.
– Matias Barrios
Nov 23 '18 at 18:56
MongoNetworkError: failed to connect to server- The application cannot connect to your MongoDB server. This is a networking configuration problem and not related to the code in the question. The unhandled promise rejection calls are because you are callingmongoose.connect()without using a.catch()or alternately inside atry..catchblock with async/await. You should fix that code, but the real problem here is networking.– Neil Lunn
Nov 24 '18 at 2:10
try to restart your mongodb service by: sudo service mongod start
– Raunik Singh
Nov 24 '18 at 15:52