Unable to connect to MongoHQ using Node.js + Expressjs - node.js

I've recently been trying to learn both Node.js and Mongo. I set up an account on Heroku, got a simple Node.js app running correctly and then took a dive into Mongo. I added on MongoHQ, but since then have been unable to connect the app to the database. Every time I run the app it crashes. At this point I just want to figure out what is wrong with my db connection or my use of mongoskin. Here is my current code:
var express = require('express');
var mongo = require('mongoskin');
var app = express.createServer(express.logger());
var conn = mongo.db(process.env.MONGOHQ_URL);
app.get('/', function(request, response) {
conn.collection('facts').find().toArray(function(err, facts){
if(err) throw err;
response.send(facts);
});
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
and my package file:
{
"name": "app1",
"version": "0.0.4",
"dependencies": {
"express": "3.x",
"mongoskin" : "0.1.x"
},
"engines": {
"node": "0.8.x",
"npm": "1.1.x"
}
}
and lastly the error code:
2012-12-11T17:42:32+00:00 app[web.1]: at Module.load (module.js:356:32)
2012-12-11T17:42:32+00:00 app[web.1]: at Module._compile (module.js:449:26)
2012-12-11T17:42:32+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/mongoskin/lib/mongoskin/db.js:162:28)
2012-12-11T17:42:32+00:00 app[web.1]: at Db.Object.defineProperty.get [as state] (/app/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/db.js:2125:31)
2012-12-11T17:42:32+00:00 app[web.1]: at Module.require (module.js:362:17)
2012-12-11T17:42:32+00:00 app[web.1]: at Module._compile (module.js:449:26)
2012-12-11T17:42:32+00:00 app[web.1]: TypeError: Cannot read property '_serverState' of undefined
2012-12-11T17:42:32+00:00 app[web.1]: at Function.Module._load (module.js:312:12)
2012-12-11T17:42:32+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/mongoskin/lib/mongoskin/index.js:2:10)
2012-12-11T17:42:32+00:00 app[web.1]:
2012-12-11T17:42:32+00:00 app[web.1]: at Object.Module._extensions..js (module.js:467:10)
2012-12-11T17:42:32+00:00 app[web.1]: at require (module.js:378:17)
2012-12-11T17:42:33+00:00 heroku[web.1]: Process exited with status 1
2012-12-11T17:42:33+00:00 heroku[web.1]: State changed from starting to crashed
Any help on what might be causing the problem would be highly appreciated.

You need to just update mongoskin library. Replace this line:
"mongoskin" : "0.1.x"
with
"mongoskin" : ">= 0.3.6"
More details here

Related

can't get heroku to find my custom module in node.js

I'm developing an API in node.js that's deployed to heroku, that uses a custom module to fetch weather information from another API.
Here is the App Tree
Here is my app.js
const data_lluvias = require('./data_lluvias');
...
app.post('/aguas_lluvia', (req, res) => {
let data = data_lluvias.getDataLluvias;
res.json(data);
});
app.listen(port, () => {
console.log('Server running');
});
This is the weather module
const url = 'https://climatologia.meteochile.gob.cl/application/productos/boletinClimatologicoDiario';
const getDataLluvias = async () => {
const response = await fetch(url, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
const data = response.json();
return data;
};
module.exports = {
getDataLluvias
};
I can't get heroku to find the module that is in the same folder that my app.js file
Here's the console log
2022-07-16T02:56:32.844556+00:00 heroku[web.1]: State changed from crashed to starting
2022-07-16T02:56:34.518501+00:00 heroku[web.1]: Starting process with command `npm start`
2022-07-16T02:56:36.166569+00:00 app[web.1]:
2022-07-16T02:56:36.166608+00:00 app[web.1]: > conectamilk-npurin#1.0.0 start
2022-07-16T02:56:36.166610+00:00 app[web.1]: > node app.js
2022-07-16T02:56:36.166610+00:00 app[web.1]:
2022-07-16T02:56:36.324024+00:00 app[web.1]: node:internal/modules/cjs/loader:936
2022-07-16T02:56:36.324025+00:00 app[web.1]: throw err;
2022-07-16T02:56:36.324026+00:00 app[web.1]: ^
2022-07-16T02:56:36.324026+00:00 app[web.1]:
2022-07-16T02:56:36.324026+00:00 app[web.1]: Error: Cannot find module './data_lluvias'
2022-07-16T02:56:36.324027+00:00 app[web.1]: Require stack:
2022-07-16T02:56:36.324027+00:00 app[web.1]: - /app/app.js
2022-07-16T02:56:36.324042+00:00 app[web.1]: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-07-16T02:56:36.324044+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-07-16T02:56:36.324044+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-07-16T02:56:36.324044+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)
2022-07-16T02:56:36.324045+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:6:22)
2022-07-16T02:56:36.324045+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1105:14)
2022-07-16T02:56:36.324045+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
2022-07-16T02:56:36.324046+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32)
2022-07-16T02:56:36.324046+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-07-16T02:56:36.324046+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
2022-07-16T02:56:36.324047+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-07-16T02:56:36.324047+00:00 app[web.1]: requireStack: [ '/app/app.js' ]
2022-07-16T02:56:36.324047+00:00 app[web.1]: }
2022-07-16T02:56:36.449409+00:00 heroku[web.1]: Process exited with status 1
2022-07-16T02:56:36.545505+00:00 heroku[web.1]: State changed from starting to crashed
2022-07-16T02:56:36.550346+00:00 heroku[web.1]: State changed from crashed to starting
I've been trying different paths in the require but none worked.
Can anyone point me the right direction please?
Thanks

mdb with node js hosted on Heroku does not work

I used 'node-adodb' nodejs module with .mdb database. Here is my code.
const ADODB = require('node-adodb');
const connection = ADODB.open(`Provider=Microsoft.Jet.OLEDB.4.0;Data Source=${__dirname}/data/Database.mdb;`);
module.exports = { connection : connection };
This is worked on a local and throws the following error after hosted on Heroku.
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
2020-04-23T21:53:16.421683+00:00 app[web.1]: ^
2020-04-23T21:53:16.421683+00:00 app[web.1]:
2020-04-23T21:53:16.421684+00:00 app[web.1]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
2020-04-23T21:53:16.421684+00:00 app[web.1]: at validateString (internal/validators.js:117:11)
2020-04-23T21:53:16.421685+00:00 app[web.1]: at Object.join (path.js:1039:7)
2020-04-23T21:53:16.421686+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/node-adodb/lib/engine.js:16:24)
2020-04-23T21:53:16.421686+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1156:30)
2020-04-23T21:53:16.421687+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
2020-04-23T21:53:16.421687+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:1000:32)
2020-04-23T21:53:16.421687+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:899:14)
2020-04-23T21:53:16.421688+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:1042:19)
2020-04-23T21:53:16.421688+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:77:18)
2020-04-23T21:53:16.421688+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/node-adodb/index.js:11:16) {
2020-04-23T21:53:16.421689+00:00 app[web.1]: code: 'ERR_INVALID_ARG_TYPE'
2020-04-23T21:53:16.421689+00:00 app[web.1]: }

mongoose.connect not invoking callback after connection to mongodb

I'm trying to make a connection to MongoDB using Mongoose. But it does neither throw a error nor connect to the database. Following is my code.
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const mongoose = require('mongoose');
console.log('Hi, there!!');
mongoose.connect('mongodb://localhost:27017/db_name', (err) => {
console.log('Callback called');
if(err) throw err;
console.log('Connected to database');
})
In the above code none of the console.log inside the callback do happen. But any place outside the mongoose.connect do work like console.log('Hi, there!!')
Versions Used
express: 4.0.0
mongoose: 3.8.40
node: 7.7.3
mongodb: 3.4.0
Using mongoose: 3.8.40 I got this in the console :
{ Error: Cannot find module '../build/Release/bson'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/kevin/nemeacreation/sites/test/stackoverflow/node_modules/bson/ext/index.js:15:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3) code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
Hi, there!!
upgrading to "mongoose": "~4.4" fixed it for me. I got the answer here : https://stackoverflow.com/a/35516644/2829540
For info the latest release of mongoose is 4.10.4

Syntax error on express app.use

I have used this code with my node.js application with express but when uploading to Heroku I get an error when running.
2016-09-28T11:01:40.502448+00:00 app[web.1]: /app/routes/index.js:146
2016-09-28T11:01:40.502467+00:00 app[web.1]: app.use(({method, url}, rsp, next) => {
2016-09-28T11:01:40.502468+00:00 app[web.1]: ^
2016-09-28T11:01:40.502468+00:00 app[web.1]:
2016-09-28T11:01:40.502469+00:00 app[web.1]: SyntaxError: Unexpected token {
2016-09-28T11:01:40.502469+00:00 app[web.1]: at exports.runInThisContext (vm.js:53:16)
2016-09-28T11:01:40.502470+00:00 app[web.1]: at Module._compile (module.js:387:25)
2016-09-28T11:01:40.502471+00:00 app[web.1]: at Object.Module._extensions..js (module.js:422:10)
2016-09-28T11:01:40.502471+00:00 app[web.1]: at Module.load (module.js:357:32)
2016-09-28T11:01:40.502472+00:00 app[web.1]: at Function.Module._load (module.js:314:12)
2016-09-28T11:01:40.502473+00:00 app[web.1]: at Module.require (module.js:367:17)
2016-09-28T11:01:40.502473+00:00 app[web.1]: at require (internal/module.js:20:19)
2016-09-28T11:01:40.502474+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:8:14)
2016-09-28T11:01:40.502474+00:00 app[web.1]: at Module._compile (module.js:413:34)
2016-09-28T11:01:40.502475+00:00 app[web.1]: at Object.Module._extensions..js (module.js:422:10)
My code is
app.use(({method, url}, rsp, next) => {
rsp.on('finish', () => {
console.log(`${rsp.statusCode} ${method} ${url}`);
});
next();
});
Any help would be much appreciated
In your package.json, do you specify the node engine?
"engines": {
"node": "6.5.0"
}
I would have just added this as a comment, but I don't have enough rep.

app not defined error while deploying through modulus

While trying to deploy my node app using modulus, I encounter the error message
ReferenceError: app is not defined
at Object.<anonymous> (/mnt/app/index.js:1:63)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)
at startup (node.js:140:9)
at node.js:463:3
index.js:
var express = require('express');
var app = express();
app.get('/hello', function(req, res) {
console.log("World!!");
})
Express.js has been installed and added to "dependencies" in package.json
"dependencies": {
"express": "^4.14.0"
}
What could be the probable cause for the Error.
app.listen(process.env.PORT || 3000);
Missed out on listening to the environment variable PORT that is causing the issue.

Resources