'mongodb atalis not allowing me to make a get request
I am working on my first full stack app, however mongodb atalis is not letting me make any requests. It will just keep loading until it time out without an error and my code seems correct. Can anyone help me to figure out what's wrong? I have been asking my instructors, and they aren't able to find out whats wrong, and i have no clue what to do. It was also working previously but for some reason just broke and i can't even make my requests on postman Here is my code :
Schema:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const bountySchema = new Schema({
firstName: {
type: String,
required: true
},
lastName:{
type: String,
required:true
},
living:{
type: Boolean,
},
bounty:{
type:Number,
required:true
},
type: String
})
module.exports = mongoose.model("Bounty", bountySchema)
routes:
const express = require('express')
const bountyRouter = express.Router()
const Bounty = require('../models/bounty')
bountyRouter.route('/')
.get((req, res, next) => {
Bounty.find((err, bounties) => {
if (err) {
res.status(500)
return next(err)
}
return res.status(200).send(bounties)
})
})
.post((req, res,next)=>{
const newBounty = new Bounty(req.body)
newBounty.save((err, savedBounty)=>{
if(err){
res.status(500)
return next(err)
}
return res.status(200).send(savedBounty)
})
})
bountyRouter.get('/:bountyId', (req, res) => {
const bountyId = req.params.bountyId
const foundBounty = bounty.find(bounty => bounty._id === bountyId)
res.send(foundBounty)
console.log(req)
})
// bountyRouter.delete('/:bountyId', (req, res) => {
// const bountyId = req.params.bountyId
// const bountyIndex = bounty.findIndex(bount => bount.id === bountyId)
// bounty.splice(bountyIndex, 1)
// res.send("goodbye bounty!")
// })
bountyRouter.delete('/:bountyId', (req,res,next)=>{
Bounty.findOneAndRemove(req.params.bountyId, (err, deletedBounty)=>{
if(err){
res.status(500)
return next(err)
}
return res.status(200).send(`Successfully deleted ${deletedBounty.firstName}`)
})
})
// bountyRouter.put('/:bountyId', (req, res) => {
// const bountyId = req.params.bountyId;
// const bountyIndex = bounty.findIndex((bounty) => bounty._id === bountyId);
// const updatedBounty = Object.assign(bounty[bountyIndex], req.body);
// if (!updatedBounty) {
// const error = new Error(`something went wrong`)
// res.status(500)
// return next(error)
// }
// res.status(201).send(updatedBounty);
// })
bountyRouter.put('/:bountyId',(req, res, next)=>{
Bounty.findOneAndUpdate(req.params.bountyId , req.body, {new:true},(err,updateBounty)=>{
if(err){
res.status(500).send(err)
return next(err)
}
return res.status(200).send(updateBounty)
})
})
bountyRouter.get('/search/type', (req, res) => {
const type = req.query.type
const filteredType = bounty.filter(bount => bount.type === type)
res.send(filteredType)
console.log(req)
})
module.exports = bountyRouter
server:
const express = require('express')
const mongoose = require('mongoose')
mongoose.connect("mongodb+srv://mjones89:[email protected]/bounty?retryWrites=true&w=majority", ()=>{
console.log('success')
})
const app = express()
const bodyParser =require('body-parser')
app.use(bodyParser.json())
app.use('/bounty', require('./routes/bounties.js'))
app.use((err, req,res,next)=>{
console.log(err)
return res.send({errMsg: err.message})
})
app.listen(9000, ()=>{
console.log('bounty hunter')
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
