Sunday, 17 January 2021

mongoose post.save failing on heroku, works on localhost

I'm trying to simply post to my MongoDB Atlas db via node,express,mongoose and heroku. A Postman POST request, Raw JSON with body:

{
    "title": "heroku post",
    "description": "post me plsssss"
}

works on localhost with this exact code, but when uploaded via heroku the try/catch block fails at post.save() as the response is the error.

{
    "error": "there's an error",
    "message": {}
}

But the error is empty and I'm not sure how to debug it. I've put in mongoose.set('debug', true); in app.js and i've modified my package.json: "start": "node app.js DEBUG=mquery", but those have made no extra output that I am seeing. Is there any other way to know why the post.save() is throwing an error, some logs that I am not utilising.. and how can I see those logs? Or if you just know what the issue is?

App.js

require("dotenv").config();
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const cors = require('cors');

mongoose.set('debug', true);

//MIDDLEWARES
//cors
app.use(cors());
//decode url special characters
app.use(express.urlencoded({ extended: true }));
//parse json POSTs
app.use(express.json());

//import routes for middleware
const postsRoute = require('./routes/posts');

//midleware routes
app.use('/posts', postsRoute)

//ROUTES
app.get('/', (req,res) => {
  res.send('we are on home')
})

//connect to DB
mongoose.connect("mongodb+srv://xxxxx:xxxxx@cluster-rest.4luv0.mongodb.net/cluster-rest?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true, dbName: "cluster-rest" }, () => {
  console.log('connected to DB!')
})

app.listen( process.env.PORT || 3000);

posts.js

const express = require ('express')
const router = express.Router();
const Post = require('../models/Post')

//SUBMIT A POST
router.post('/', async (req,res) => {
  const post = new Post({
    title: req.body.title,
    description: req.body.description
  });

  console.log(post)

  try {
    const savedPost = await post.save();
    res.json(savedPost);
    console.log(savedPost)
  } catch (err) {
    res.json({ error: "there's an error", message: err, })
  }
})

module.exports = router;

Post.js Model

const mongoose = require('mongoose')
const PostSchema = mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  }
})
module.exports = mongoose.model('Post', PostSchema)

When I type heroku logs --tail there are no errors, also initially, the 'connected to DB!' message comes in a bit late.. I'm wondering if maybe this is an issue with async/await? My package.json:

{
  "name": "22-npmexpressrestapi",
  "version": "1.0.0",
  "engines": {
    "node": "14.15.3",
    "npm": "6.14.9"
  },
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node app.js DEBUG=mquery",
    "start:dev": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.7"
  },
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "4.17.1",
    "cors": "^2.8.5",
    "mongoose": "^5.11.11"
  }
}


from mongoose post.save failing on heroku, works on localhost

No comments:

Post a Comment