Heroku would not connect to MongoDB Atlas - node.js

I know this question had been asked before, but I'm seriously not finding an answer to this issue anywhere.
I deployed a Node.js app to Heroku successfully, but I cannot connect to my MongoDB Atlas cluster.
This is the error I get:
2020-04-07T10:39:20.723516+00:00 app[web.1]: Connection failed... MongooseError [MongooseServerSelectionError]: connection <monitor> to 54.72.180.237:27017 closed
2020-04-07T10:39:20.723526+00:00 app[web.1]: at new MongooseServerSelectionError (/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
2020-04-07T10:39:20.723527+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:808:32)
2020-04-07T10:39:20.723529+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:333:15)
2020-04-07T10:39:20.723530+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:22:4)
2020-04-07T10:39:20.723531+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:959:30)
2020-04-07T10:39:20.723531+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
2020-04-07T10:39:20.723532+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:815:32)
2020-04-07T10:39:20.723532+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:727:14)
2020-04-07T10:39:20.723532+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
2020-04-07T10:39:20.723533+00:00 app[web.1]: at internal/main/run_main_module.js:17:11 {
2020-04-07T10:39:20.723533+00:00 app[web.1]: message: 'connection <monitor> to 54.72.180.237:27017 closed',
2020-04-07T10:39:20.723534+00:00 app[web.1]: name: 'MongooseServerSelectionError',
2020-04-07T10:39:20.723534+00:00 app[web.1]: reason: TopologyDescription {
2020-04-07T10:39:20.723535+00:00 app[web.1]: type: 'ReplicaSetNoPrimary',
2020-04-07T10:39:20.723535+00:00 app[web.1]: setName: null,
2020-04-07T10:39:20.723535+00:00 app[web.1]: maxSetVersion: null,
2020-04-07T10:39:20.723536+00:00 app[web.1]: maxElectionId: null,
2020-04-07T10:39:20.723536+00:00 app[web.1]: servers: Map {
2020-04-07T10:39:20.723537+00:00 app[web.1]: 'milenskiblog-shard-00-00-lkwi4.mongodb.net:27017' => [ServerDescription],
2020-04-07T10:39:20.723537+00:00 app[web.1]: 'milenskiblog-shard-00-01-lkwi4.mongodb.net:27017' => [ServerDescription],
2020-04-07T10:39:20.723538+00:00 app[web.1]: 'milenskiblog-shard-00-02-lkwi4.mongodb.net:27017' => [ServerDescription]
2020-04-07T10:39:20.723539+00:00 app[web.1]: },
2020-04-07T10:39:20.723539+00:00 app[web.1]: stale: false,
2020-04-07T10:39:20.723539+00:00 app[web.1]: compatible: true,
2020-04-07T10:39:20.723540+00:00 app[web.1]: compatibilityError: null,
2020-04-07T10:39:20.723540+00:00 app[web.1]: logicalSessionTimeoutMinutes: null,
2020-04-07T10:39:20.723541+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2020-04-07T10:39:20.723541+00:00 app[web.1]: localThresholdMS: 15,
2020-04-07T10:39:20.723541+00:00 app[web.1]: commonWireVersion: null
2020-04-07T10:39:20.723542+00:00 app[web.1]: },
2020-04-07T10:39:20.723542+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {}
2020-04-07T10:39:20.723542+00:00 app[web.1]: }
This is how I connect to MongoDB:
mongoose
.connect(process.env.blog_db, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to MongoDB..."))
.catch((err) => console.error(`Connection failed...`, err));
And this is my environment variable (which I set exactly the same in Heroku).
The weird thing is, if I run nodemon index.js the app runs perfectly and it connects to the MongoDB Atlas cluster immediately, but the deployed heroku app doesnt seem to connect to the DB.
I am literally out of ideas and I'm plucking my hair here, so please if anyone had tackled this issue before, I need your help!
Thanks in advance!

Related

Error in Connecting the MongoDB server with Node app MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

I am working on a MERN project and Earlier my mongo server was working fine, but now that I have restarted my app it is giving me error i.e is
`
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/connection.js:824:32)
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:381:10
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:1234:10)
at Mongoose.connect (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:380:20)
at connectDataBase (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/config/dataBase.js:7:10)
at Object.<anonymous> (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/server.js:17:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
My confige.env file looks like this
PORT = 4000
DB_URI = "mongodb://localhost:27017/Ecommerce"
JWT_SECRET=JBRON098SROINRIOUHOHSDOKFN948HIFRCN
JWT_EXPIRE=5d
COOKIE_EXPIRE = 5
My app.js file looks like this
const app = require('./app')
const dotenv = require('dotenv')
const connectDataBase = require("./config/dataBase")
//Handling uncaught exception
process.on("uncaughtException", (err) => {
console.log(`Error: , ${err.message}`);
console.log(`Shutting down server due to unhandled promise rejection`);
process.exit(1);
})
//config file
dotenv.config({path:"backend/config/config.env"});
//Calling the function to connect the database
connectDataBase();
const server = app.listen(process.env.PORT, ()=>{
console.log(`The server has started at http://localhost/${process.env.PORT}`);
})
I have also tried connecting with compass but it is still not working. I used a MacBook air m2 with macOS Ventura 13.0.1
I wanted to connect to the server using the app only but it is giving me the error
`
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/connection.js:824:32)
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:381:10
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:1234:10)
at Mongoose.connect (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:380:20)
at connectDataBase (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/config/dataBase.js:7:10)
at Object.<anonymous> (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/server.js:17:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
`

I have been trying to link my node to Mongo Db to local host but it keep throwing me error

const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://localhost:27017/test');
}
eroor
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807:32) at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:340:10
at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:1140:10)
at Mongoose.connect (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:339:20)
at main (E:\Web Dev\Backend\New folder\index.js:5:18)
at Object.<anonymous> (E:\Web Dev\Backend\New folder\index.js:3:1)
at Module._compile (node:internal/modules/cjs/loader:1097:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
Enabling the IPV6 that MongoDB has disabled by default, by using the given command also I'm adding a reference for your understanding. mongod --ipv6
https://docs.mongodb.com/manual/core/security-mongodb-configuration/
I tried with the given piece of code and its working now,
mongoose.connect('mongodb://localhost:27017/db_name',
{
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
}
);
https://www.section.io/engineering-education/nodejs-mongoosejs-mongodb/

Can't connect to mongodb atlas on nodejs

mongoose.connect(
process.env.DB_CONFIG,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
}
)
.then((x) => {
console.log(
`Connected to MongoDB!`
);
})
.catch((err) => {
console.error("Error connecting to mongo", err);
});
This is the piece of code I have been working on and this works fine until this day.
I don't know why and got an error like this:
Error connecting to mongo MongooseServerSelectionError: connection timed out
at NativeConnection.Connection.openUri (D:\projects\medium-clone\server\node_modules\mongoose\lib\connection.js:846:32)
at D:\projects\medium-clone\server\node_modules\mongoose\lib\index.js:351:10
at D:\projects\medium-clone\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (D:\projects\medium-clone\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (D:\projects\medium-clone\server\node_modules\mongoose\lib\index.js:1149:10)
at Mongoose.connect (D:\projects\medium-clone\server\node_modules\mongoose\lib\index.js:350:20)
at Object.<anonymous> (D:\projects\medium-clone\server\index.js:12:4)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: 'atlas-vwy17h-shard-0',
maxSetVersion: null,
maxElectionId: null,
servers: Map(3) {
'authcluster-shard-00-00.ris23.mongodb.net:27017' => [ServerDescription],
'authcluster-shard-00-01.ris23.mongodb.net:27017' => [ServerDescription],
'authcluster-shard-00-02.ris23.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: 30,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: 9
}
}
I tried adding my ip to network access in mongodb dashboard but didn't work
Sometimes this happens because of your own internet connection when you use mobile hotspot. In this case adding your IP may never work unfortunately because your IP may probably change every time you reconnect. When I had the same issue I restarted my connection and allowed all IPs in access network. And also make sure that your connection is stable.

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted.
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\connection.js:832:32)
at C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\index.js:345:10
at C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose._promiseOrCallback (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\index.js:1135:10)
at Mongoose.connect (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\index.js:344:20)
at Object.<anonymous> (C:\Users\Admin\Desktop\Working\SB\app.js:305:10) at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map {
'smile-shard-00-00.xeyqd.mongodb.net:27017' => [ServerDescription],
'smile-shard-00-01.xeyqd.mongodb.net:27017' => [ServerDescription],
'smile-shard-00-02.xeyqd.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
I have already update IP address to mongodb network access => IP Access list to 0.0.0.0/0
But the error has still occurred. I think this error is occurring due to the window firewall. But I have not any idea to resolve this.

Getting A “Cast to ObjectId” Error With Heroku, But Not With LocalHost?

My 'lessons/new' page loads fine on my localhost, but gives an error with heroku. The page will load, but it crashes immediately after in heroku. I added a console.log(foundClass) for getClassesById and the foundClass dislplayed correctly. Can someone please point me in the right direction?
class.js lines 27-33
module.exports.getClassesById = function(id, callback){
Class.findById(id)
.populate(
{path: 'lessons',
model: 'Lesson', $ne: null}
).exec(callback)
}
classes.js
router.get('/:id/lessons/new', function(req, res, next) {
Class.getClassesById([req.params.id], function(err, foundClass){
if (err){
console.log(err)
throw err
} else {
if (req.user && req.user.email == foundClass.instructor_email){
res.render('lessons/new', {class_id: req.params.id})
} else {
res.redirect('/');
}
}
})
});
heroku logs
2016-10-24T17:31:08.413872+00:00 app[web.1]: [0mGET /classes/580e0e820a1e5d00118e6159/lessons/new [36m304 [0m30.674 ms - -[0m
2016-10-24T17:31:08.486402+00:00 app[web.1]: [0mGET /stylesheets/bootstrap.min.css [36m304 [0m2.559 ms - -[0m
2016-10-24T17:31:08.486823+00:00 app[web.1]: [0mGET /stylesheets/style.css [36m304 [0m2.326 ms - -[0m
2016-10-24T17:31:08.487513+00:00 app[web.1]: [0mGET /stylesheets/ie10-viewport-bug-workaround.css [36m304 [0m2.315 ms - -[0m
2016-10-24T17:31:08.489252+00:00 app[web.1]: [0mGET /stylesheets/jumbotron.css [36m304 [0m3.708 ms - -[0m
2016-10-24T17:31:08.529026+00:00 app[web.1]: [0mGET /js/bootstrap.min.js [36m304 [0m2.226 ms - -[0m
2016-10-24T17:31:08.530994+00:00 app[web.1]: [0mGET /js/ie10-viewport-bug-workaround.js [36m304 [0m0.699 ms - -[0m
2016-10-24T17:31:09.037191+00:00 app[web.1]: { CastError: Cast to ObjectId failed for value "[ 'favicon.ico' ]" at path "_id"
2016-10-24T17:31:09.037205+00:00 app[web.1]: at MongooseError.CastError (/app/node_modules/mongoose/lib/error/cast.js:26:11)
2016-10-24T17:31:09.037206+00:00 app[web.1]: at ObjectId.cast (/app/node_modules/mongoose/lib/schema/objectid.js:147:13)
2016-10-24T17:31:09.037207+00:00 app[web.1]: at ObjectId.castForQuery (/app/node_modules/mongoose/lib/schema/objectid.js:187:15)
2016-10-24T17:31:09.037208+00:00 app[web.1]: at cast (/app/node_modules/mongoose/lib/cast.js:225:32)
2016-10-24T17:31:09.037209+00:00 app[web.1]: at Query.cast (/app/node_modules/mongoose/lib/query.js:2737:10)
2016-10-24T17:31:09.037209+00:00 app[web.1]: at Query.findOne (/app/node_modules/mongoose/lib/query.js:1350:10)
2016-10-24T17:31:09.037210+00:00 app[web.1]: at /app/node_modules/mongoose/lib/query.js:2296:21
2016-10-24T17:31:09.037210+00:00 app[web.1]: at new Promise.ES6 (/app/node_modules/mongoose/lib/promise.js:45:3)
2016-10-24T17:31:09.037211+00:00 app[web.1]: at Query.exec (/app/node_modules/mongoose/lib/query.js:2290:17)
2016-10-24T17:31:09.037211+00:00 app[web.1]: at Function.module.exports.getClassesById (/app/models/class.js:32:4)
2016-10-24T17:31:09.037212+00:00 app[web.1]: at /app/routes/classes.js:105:8
2016-10-24T17:31:09.037213+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:82:5)
2016-10-24T17:31:09.037213+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:110:13)
2016-10-24T17:31:09.037214+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:91:3)
2016-10-24T17:31:09.037214+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:82:5)
2016-10-24T17:31:09.037214+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:267:22
2016-10-24T17:31:09.037215+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:340:14)
2016-10-24T17:31:09.037215+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:356:14)
2016-10-24T17:31:09.037216+00:00 app[web.1]: at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:400:3)
2016-10-24T17:31:09.037216+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:261:10)
2016-10-24T17:31:09.037216+00:00 app[web.1]: at Function.proto.handle (/app/node_modules/express/lib/router/index.js:166:3)
2016-10-24T17:31:09.037217+00:00 app[web.1]: at router (/app/node_modules/express/lib/router/index.js:35:12)
2016-10-24T17:31:09.037217+00:00 app[web.1]: message: 'Cast to ObjectId failed for value "[ \'favicon.ico\' ]" at path "_id"',
2016-10-24T17:31:09.037218+00:00 app[web.1]: name: 'CastError',
2016-10-24T17:31:09.037218+00:00 app[web.1]: kind: 'ObjectId',
2016-10-24T17:31:09.037218+00:00 app[web.1]: value: [ 'favicon.ico' ],
2016-10-24T17:31:09.037219+00:00 app[web.1]: path: '_id',
2016-10-24T17:31:09.037219+00:00 app[web.1]: reason: undefined }
2016-10-24T17:31:09.038731+00:00 app[web.1]:
2016-10-24T17:31:09.038733+00:00 app[web.1]: events.js:160
2016-10-24T17:31:09.038734+00:00 app[web.1]: throw er; // Unhandled 'error' event
2016-10-24T17:31:09.038734+00:00 app[web.1]: ^
2016-10-24T17:31:09.038768+00:00 app[web.1]: CastError: Cast to ObjectId failed for value "[ 'favicon.ico' ]" at path "_id"
2016-10-24T17:31:09.038769+00:00 app[web.1]: at MongooseError.CastError (/app/node_modules/mongoose/lib/error/cast.js:26:11)
2016-10-24T17:31:09.038770+00:00 app[web.1]: at ObjectId.cast (/app/node_modules/mongoose/lib/schema/objectid.js:147:13)
2016-10-24T17:31:09.038771+00:00 app[web.1]: at ObjectId.castForQuery (/app/node_modules/mongoose/lib/schema/objectid.js:187:15)
2016-10-24T17:31:09.038771+00:00 app[web.1]: at cast (/app/node_modules/mongoose/lib/cast.js:225:32)
2016-10-24T17:31:09.038772+00:00 app[web.1]: at Query.cast (/app/node_modules/mongoose/lib/query.js:2737:10)
2016-10-24T17:31:09.038773+00:00 app[web.1]: at Query.findOne (/app/node_modules/mongoose/lib/query.js:1350:10)
2016-10-24T17:31:09.038773+00:00 app[web.1]: at /app/node_modules/mongoose/lib/query.js:2296:21
2016-10-24T17:31:09.038774+00:00 app[web.1]: at new Promise.ES6 (/app/node_modules/mongoose/lib/promise.js:45:3)
2016-10-24T17:31:09.038774+00:00 app[web.1]: at Query.exec (/app/node_modules/mongoose/lib/query.js:2290:17)
2016-10-24T17:31:09.038775+00:00 app[web.1]: at Function.module.exports.getClassesById (/app/models/class.js:32:4)
2016-10-24T17:31:09.038776+00:00 app[web.1]: at /app/routes/classes.js:105:8
2016-10-24T17:31:09.038776+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:82:5)
2016-10-24T17:31:09.038777+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:110:13)
2016-10-24T17:31:09.038778+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:91:3)
2016-10-24T17:31:09.038778+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:82:5)
2016-10-24T17:31:09.038779+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:267:22
2016-10-24T17:31:09.038779+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:340:14)
2016-10-24T17:31:09.038780+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:356:14)
2016-10-24T17:31:09.038780+00:00 app[web.1]: at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:400:3)
2016-10-24T17:31:09.038781+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:261:10)
2016-10-24T17:31:09.038781+00:00 app[web.1]: at Function.proto.handle (/app/node_modules/express/lib/router/index.js:166:3)
2016-10-24T17:31:09.038782+00:00 app[web.1]: at router (/app/node_modules/express/lib/router/index.js:35:12)
2016-10-24T17:31:09.051877+00:00 app[web.1]:
2016-10-24T17:31:09.059912+00:00 app[web.1]: npm ERR! Linux 3.13.0-100-generic
2016-10-24T17:31:09.060095+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2016-10-24T17:31:09.060223+00:00 app[web.1]: npm ERR! node v6.6.0
2016-10-24T17:31:09.060321+00:00 app[web.1]: npm ERR! npm v3.10.3
2016-10-24T17:31:09.060423+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2016-10-24T17:31:09.060516+00:00 app[web.1]: npm ERR! eTeacher#0.0.1 start: `node ./bin/www`
2016-10-24T17:31:09.060581+00:00 app[web.1]: npm ERR! Exit status 1
2016-10-24T17:31:09.060664+00:00 app[web.1]: npm ERR!
2016-10-24T17:31:09.060738+00:00 app[web.1]: npm ERR! Failed at the eTeacher#0.0.1 start script 'node ./bin/www'.
2016-10-24T17:31:09.060808+00:00 app[web.1]: npm ERR! Make sure you have the latest version of node.js and npm installed.
2016-10-24T17:31:09.060873+00:00 app[web.1]: npm ERR! If you do, this is most likely a problem with the eTeacher package,
2016-10-24T17:31:09.060940+00:00 app[web.1]: npm ERR! not with npm itself.
2016-10-24T17:31:09.061016+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2016-10-24T17:31:09.061085+00:00 app[web.1]: npm ERR! node ./bin/www
2016-10-24T17:31:09.061153+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2016-10-24T17:31:09.061212+00:00 app[web.1]: npm ERR! npm bugs eTeacher
2016-10-24T17:31:09.061280+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2016-10-24T17:31:09.061347+00:00 app[web.1]: npm ERR! npm owner ls eTeacher
2016-10-24T17:31:09.061415+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2016-10-24T17:31:09.065020+00:00 app[web.1]:
2016-10-24T17:31:09.065328+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2016-10-24T17:31:09.065465+00:00 app[web.1]: npm ERR! /app/npm-debug.log
Class schema
var classSchema = new Schema({
title: { type: String, required: true },
description: { type: String, required: true },
instructor: { type: String, required: true },
instructor_email: { type: String, required: true },
//Lessons for the class
lessons: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'Lesson' }
],
category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
created : { type : Date, default : Date.now }
});
'classes/show' is the page it is coming from
classes.js 104-114
router.get('/:id', function(req, res, next) {
Class.getClassesById([req.params.id], function(err, foundClass){
if (err){
console.log(err)
throw err
} else {
console.log('Found class.')
res.render('classes/show', {"class": foundClass})
}
});
});
It's giving that error because Mongo ID is supposed to be of a specific format. The error log says that it got "favicon.ico" as a param which violates the Mongo ID format.
You should validate the data ALWAYS before doing DB operations.
There's a package for string validation and sanitization: https://www.npmjs.com/package/validator
You can check if the param is in proper format by:
var validator = require('validator');
then in your route:
if (!validator.isMongoId(req.params.id)) {
// Param is not mongoId, throw error
}

Resources