Error while running js file via node - node.js

I was trying to run my koa app.js file but I could not get it to work.
Here's my app.js
var koa = require('koa'),
monk = require('monk'),
wrap = require('co-monk');
var db = monk("mongodb url here"),
collection = db.get('mycollection'),
transactions = wrap(collection);
var app = koa();
app.use(function*(){
var res = yield transactions.find({});
console.log(res);
});
app.listen(3000);
and here's the error i got:
TypeError: Cannot read property 'name' of undefined
at makeSkinClass (/home/ric/node_modules/mongoskin/lib/utils.js:33:43)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/grid.js:6:35)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/db.js:22:16)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/mongo_client.js:5:14)
I'm still new to Koa,nodejs so I'm having a hard time figuring this out. Any help would be great!

Related

Getting Error while using razorpay in file

Whenever I do
const Razorpay = require('razorpay');
There's an error coming up in node_modules/razorpay during running the server
node_modules\request-promise-core\configure\request2.js:34
var originalInit = options.request.Request.prototype.init;
^
TypeError: Cannot read property 'prototype' of undefined
at module.exports (node_modules\request-promise-core\configure\request2.js:34:48)
at Object. (node_modules\request-promise\lib\rp.js:28:1)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object. (node_modules\razorpay\dist\api.js:7:15)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object. (node_modules\razorpay\dist\razorpay.js:7:11)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)

moongoose js Schema is not a Constructor and function error

I am facing a problem with moongoose js
This is my node js code:
var mongoose=require('moongoose');
var test=mongoose.Schema({test:String})
module.exports = mongoose.model('test',test);
When I run this code, node js throws:
var test=mongoose.Schema({test:String})
^
TypeError: **mongoose.Schema is not a function**
at Object.<anonymous> (H:\TodoList\Model\TodoListModel.js:4:19)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (H:\TodoList\app.js:9:10)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
If I change this code to this format:
var mongoose=require('moongoose');
var Schema = mongoose.Schema;
var test=new Schema({test:String})
module.exports = mongoose.model('test',test);
That code throws:
TypeError: **Schema is not a constructor**
Please help me to resolve this code error.
Its require('mongoose'); you have written require('moongoose');
an o typo :))

cannot read property of 'get' undefined in the express router

My code is as shown below:
app.js
const app = express();
const itemsRouter = require('./routes/items.js');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/items', itemsRouter);
items.js
const router = require('express').router;
router.get('/itemTest', (req, res) => {
res.json({
sucess: true
});
console.log(`the parametrs are ${req.body.item_name} ${req.body.item_post}`);
});
module.exports = router;
But here somehow, I am not able to get the router working and it says that can not read property 'get' of undefined.
the error stack is as shown below:
TypeError: Cannot read property 'get' of undefined
at Object.<anonymous> (C:\Users\anand\quFlipApi\routes\items.js:5:7)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\anand\quFlipApi\app.js:33:19)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
There are two problems here:
It's express.Router and not express.router.
You need to create a new instance of express.Router (e.g. var router = new express.Router()) instead of trying to using the constructor directly as an instance.

DATABASE_URL undefined node.js

I config DATABASE_URL in my dashboard.
When I do in my prompt heroku config
I get:
miguel#miguel-VirtualBox:~/Escritorio/mis_proyectos/quiz$ heroku config
=== quiz-miguel2015 Config Vars
DATABASE_URL: postgres://fudidvccwppmri:Ki28zZ00ikrE43UBcKT1YnQLwb#ec2-54-83-18-87.compute-1.amazonaws.com:5432/d4fh6o7fu6cv5q
HEROKU_POSTGRESQL_GOLD_URL: postgres://fudidvccwppmri:Ki28zZ00ikrE43UBcKT1YnQLwb#ec2-54-83-18-87.compute-1.amazonaws.com:5432/d4fh6o7fu6cv5q
NODE_ENV: production
but I get this fail:
undefined
/home/miguel/Escritorio/mis_proyectos/quiz/models/models.js:5
var url = process.env.DATABASE_URL.match(/(.*)\:\/\/(.*?)\:(.*)#(.*)\:(.*)\/(.
^
TypeError: Cannot call method 'match' of undefined
at Object.<anonymous>
My file where I get the fail models.js
var path = require('path');
// Postgres DATABASE_URL = postgres://user:passwd#host:port/database
// SQLite DATABASE_URL = sqlite://:#:/
console.dir(process.env.DATABASE_URL);
var url = process.env.DATABASE_URL.match(/(.*)\:\/\/(.*?)\:(.*)#(.*)\:(.*)\/(.*)/);
I have created my file .env with the following values in the root :
DATABASE_URL=sqlite://:#:/
DATABASE_STORAGE=quiz.sqlite
Here I get the complete log:
TypeError: Cannot call method 'match' of undefined
at Object.<anonymous> (/home/miguel/Escritorio/mis_proyectos/quiz/models/models.js:5:36)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/miguel/Escritorio/mis_proyectos/quiz/controllers/quiz_controller.js:2:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
the code is in => https://github.com/Mangulomx/Quiz

util.inherits throws TypeError: Cannot read property 'prototype' of undefined

app.js
var Website = new require( './jslib/website.js' );
./jslib/website.js
var util = require('util'),
events = require('events');
module.exports = Website;
function Website()
{
events.EventEmitter.call( this );
return this;
}
util.inherits( Website, events.EventEmiter );
Console Output
PATH_TO_APPDIR>node app.js
util.js:538
ctor.prototype = Object.create(superCtor.prototype, {
^
TypeError: Cannot read property 'prototype' of undefined
at Object.exports.inherits (util.js:538:43)
at Object.<anonymous> (PATH_TO_APPDIR\jslib\website.js:9:6)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at new require (module.js:378:17)
at Object.<anonymous> (PATH_TO_APPDIR\app.js:1:16)
at Module._compile (module.js:449:26)
This is NodeJS 8.22 on Windows 7*
You have a typo in your code.
util.inherits( Website, events.EventEmiter );
Should be
util.inherits( Website, events.EventEmitter );

Resources