"Home" is not a Constructor on Node JS - javascript

I'm using consign to autoload my applications models, routes and etc... I Have a Model that was Modeling in ES6 style and when I instanciate her it Throws me this error TypeError: app.src.models.Home is not a constructor
I've tried to use ES5 style but no difference. The Object that the consign create is with the models Inside but I cannot access then.
That's my class:
/*function Home() {
}
Home.prototype.getHomeData = function (conexao, callback) {
conexao.query('select * from tarefas', callback)
} //IT does not work like the item below, so I will keep using ES6
*/
class Home {
constructor() {
console.log('Construi')
}
getHomeData(conexao, callback) {
conexao.query('select * from tarefas', callback)
}
}
module.exports = function () {
return Home
}
Look:
Server.js:
var express = require('express'),
bodyparser = require('body-parser'),
app = express();
var consign = require('consign');
consign()
.include('./src/routes')
.then('./src/config/db.js')
.then('./src/models')
.into(app);
console.log("I'm on server.js: ", app.src.models)
app.use(bodyparser.urlencoded({ extended: true }));
app.listen(3000, function () {
console.log("Servidor ON");
});
module.exports = app;
and the console returns properly I'm on server.js: { 'home.model': [Function: Home] }
When I get from the Route It keeps showing me that the app.src.modelshas data,
Console output: I'm on the home.js and still have data + { 'home.model': [Function: Home] }
But When I try to Instanciate the class I throws the error cited above...
module.exports = function (app) {
app.get('/', function (req, res) {
console.log("I'm on the home.js and still have data +", app.src.models)
var conexao = app.src.config.db()
var homeData = new app.src.models.Home();
homeData.getHomeData(conexao, (err, result) => {
if (err) {
res.json(err)
} else {
res.json(result)
}
})
});
}
if I try the below the console gets undefined:
console.log("I'm on the home.js and still have data +", app.src.models.Home)
Here is my repo, if u want https://github.com/tiagosilveiraa/PM_API
Tentative 1:
On class Home i made module.exports = new Home() and it throws the same error

Export your Home class as such:
module.exports = { Home };
And import/use it as such:
const { Home } = require('path/to/Home.js');
const home = new Home();
home.getHomeData(...)

Related

express js internal server error 500 with no errors in code

my express js routes are giving me error 500 internal server error and I tried to console log the variables and nothing shows up
here are the express routes:
submitStar() {
this.app.post("/submitstar", async (req, res) => {
if(req.body.address && req.body.message && req.body.signature && req.body.star) {
const address = req.body.address;
const message = req.body.message;
const signature = req.body.signature;
const star = req.body.star;
try {
let block = await this.blockchain.submitStar(address, message, signature, star);
if(block){
return res.status(200).json(block);
} else {
return res.status(500).send("An error happened!");
}
} catch (error) {
return res.status(500).send(error);
}
} else {
return res.status(500).send("Check the Body Parameter!");
}
});
}
I keep getting the message "Check the Body Parameter!" in Postman while the message is actually correct
the app.js:
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
/**
* Require the Blockchain class. This allow us to have only one instance of the class.
*/
const BlockChain = require('./src/blockchain.js');
class ApplicationServer {
constructor() {
//Express application object
this.app = express();
//Blockchain class object
this.blockchain = new BlockChain.Blockchain();
//Method that initialized the express framework.
this.initExpress();
//Method that initialized middleware modules
this.initExpressMiddleWare();
//Method that initialized the controllers where you defined the endpoints
this.initControllers();
//Method that run the express application.
this.start();
}
initExpress() {
this.app.set("port", 8000);
}
initExpressMiddleWare() {
this.app.use(morgan("dev"));
this.app.use(bodyParser.urlencoded({extended:true}));
this.app.use(bodyParser.json());
}
initControllers() {
require("./BlockchainController.js")(this.app, this.blockchain);
}
start() {
let self = this;
this.app.listen(this.app.get("port"), () => {
console.log(`Server Listening for port: ${self.app.get("port")}`);
});
}
}
new ApplicationServer();
what could be wrong with server?
Made some changes in the code.
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
/**
* Require the Blockchain class. This allow us to have only one instance of
* the class.
*/
const BlockChain = require('./src/blockchain.js');
class ApplicationServer {
constructor() {
//Express application object
this.app = express();
//Blockchain class object
this.blockchain = new BlockChain.Blockchain();
//Method that initialized the express framework.
this.initExpress();
//Method that initialized middleware modules
this.initExpressMiddleWare();
//Method that initialized the controllers where you defined the endpoints
this.initControllers();
//Method that run the express application.
this.start();
}
initExpress() {
this.app.set("port", 8000);
}
initExpressMiddleWare() {
this.app.use(morgan("dev"));
this.router = express.Router();
this.router.use(bodyParser.urlencoded({extended:true}));
this.router.use(bodyParser.json());
}
initControllers() {
require("./BlockchainController.js")(this.app, this.blockchain);
// console.log(this.app.route(),"this.app")
}
start() {
let self = this;
this.router.post('/',(req,res) => {
console.log(req.body.id,"body");
res.status(200).send('Hello');
})
this.app.use(this.router);
this.app.listen(this.app.get("port"), (req,res) => {
console.log(`Server Listening for port: ${self.app.get("port")}`);
});
}
}
new ApplicationServer();

importing modules node.js function overrides another function even without importing

in the following code i'm using expresjs
//index.js
app.use('/',routes());
//app/routes.js
module.exports = function() {
express = require('express');
const loggedUserProfileController = require('../controllers/LoggedUserProfile');
const userProfileController = require('../controllers/userProfile');
const router = express.Router();
router.post('/get-logged-user-profile', loggedUserProfileController.getLoggedUserProfile());
router.post('/get-user-profile-data', userProfileController .getUserProfile());
return router;
}
controllers
//controllers/loggedUserProfile.js
module.exports =
{
getLoggedUserProfile:function(){
return getLoggedUserProfile:function= (req, res, next) => {
getUserCustomData();
}
}
getUserCustomData(){console.log('logged user')}
//controllers/userProfile.js
module.exports =
{
getUserProfile:function(){
return getUserProfile:function= (req, res, next) => {
getUserCustomData();
}
}
getUserCustomData(){console.log('user')}
the output is 'user'
the second getUserCustomData overrides the first one how is that possible regarding that i didn't import it in module.exports
Yes this is possible. A function definition is hoisted, declared, and assigned. If you define another in the same scope after the previous one, the new definition will now overwrite the original.
Identifiers used in function statements/definitions are not constants and are therefore subject to re-assignment. If you want it to remain static after creation you can do const getUserCustomData = function () { ... } instead and the second attempt to re-assign will throw an error for you.

Variable does not return desired object

I am building an Express app. I currently have an error in the following file, factoryRepository.js
let appReference = null;
module.exports.init = (app) => {
appReference = app;
};
module.exports.getRepositoryFactory = () => {
let repositoryFactory = {
getUserRepository: () => {
return require("./UserRepository").init(appReference.get('models').User);
}
};
return repositoryFactory;
};
appReference.get throws a TypeError because appReference is still null even after I have called module.exports.init somewhere else.
I have tried to make a function that returns appReference so I can see in what state it is. I have been able to get app rather than null, it is only in the context of getUserRepository that it stays null.
The faulty line is only called when I ping a certain route.
EDIT:
This is app.js, the context from which module.exports.init is being called
import express from 'express';
import passport from 'passport';
import config from './config/config';
let app = express();
// Setup models
app.set('models', require('./app/models'));
require('./app/repo/repositoryFactory').init(app);
// Setup config
require('./config/init')(app);
// Setup routes
require('./app/routes')(app, passport);
// Setup passport
require('./app/auth')(passport, config);
// Route to ends
require('./config/endpoints')(app);
export default app;
public.js is the logic given to my router, and it is here where I call the faulty code with repositoryFactory.getUserRepository()
let repositoryFactory = require('../repo/RepositoryFactory').getRepositoryFactory();
module.exports.doLogin = (req, res) => {
let success = () => {
res.redirect('/');
};
let error = (message) => {
res.status(500).json(message);
};
let userRepository = repositoryFactory.getUserRepository();
userRepository.findOrCreate({
facebookId: req.user.id,
displayName: req.user.displayName
}, success, error);
};
module.exports.doLogout = (req, res) => {
req.logout();
res.json({
success: true,
message: 'You\'ve succesfully logged out.'
});
};
This looks like a scope issue. Let's say we simplify your three files as follows, and also put them all in the same directory (for simplicity here):
app.js:
'use strict';
const express = require('express');
let app = express();
require('./repositoryFactory').init(app);
const myPublic = require('./public');
repositoryFactory.js:
'use strict';
let appReference = null;
module.exports.init = (app) => {
appReference = app;
};
module.exports.getRepositoryFactory = () => {
return appReference ? 'I have an app reference!' : 'No app reference!';
};
public.js:
'use strict';
let repositoryFactory = require('./RepositoryFactory').getRepositoryFactory();
console.log(repositoryFactory);
This is going to log No app reference! because the repositoryFactory is not a singleton. The repositoryFactory instance in app.js is a different instance than the repositoryFactory instance in public.js.
One solution would be to pass a parameter for the repositoryFactory instance. repositoryFactory.js would be unchanged, but app.js might look like this:
'use strict';
const express = require('express');
let app = express();
const repositoryFactory = require('./repositoryFactory')
repositoryFactory.init(app);
const myPublic = require('./public');
myPublic.init(repositoryFactory);
myPublic.log();
And the corresponding public.js might look like this:
'use strict';
let repositoryFactory = null;
module.exports.init = (myRepositoryFactory) => {
repositoryFactory = myRepositoryFactory.getRepositoryFactory();
}
module.exports.log = () => {
console.log(repositoryFactory);
}

Embed a property or method on the app object inside of controller

I´m trying associate a method or a property on a app object in kraken.js application, like follows:
controllers/index.js
'use strict';
var IndexModel = require('../models/index');
module.exports = function (app) {
var model = new IndexModel();
app.get('/', function (req, res) {
console.log(app.adventurer);
/* Console should be showing "Bilbo Bagins", but I'm getting 'undefined'.
* See the next source file. */
res.render('index', model);
});
};
/index.js
var kraken = require('kraken-js'),
app = {
adventurer: 'Bilbo Bagins'
};
app.configure = function configure(nconf, next) {
// Async method run on startup.
next(null);
};
app.requestStart = function requestStart(server) {
// Run before most express middleware has been registered.
};
app.requestBeforeRoute = function requestBeforeRoute(server) {
// Run before any routes have been added.
};
app.requestAfterRoute = function requestAfterRoute(server) {
// Run after all routes have been added.
};
if (require.main === module) {
kraken.create(app).listen(function (err) {
if (err) {
console.error(err.stack);
}
});
}
module.exports = app;
Also, i´ve tried publish the property on /config/app.json
Any Thoughts?
Simply add the following key to .config/app.json or make a new .config/app-development.json:
"adventurer": "bilbo"
app.json will look like this:
{
//blah
//blah
"adventurer": "bilbo"
}
and then in ./index.js do this in configure:
app.configure = function configure(nconf, next) {
// Async method run on startup.
next(null);
console.log('my traveler is: ', nconf.get('adventurer'));
};
In response to your comment, if you want to get an app config from the ./controllers/index.js then require the nconf lib and use nconf.get like so:
'use strict';
var nconf = require('nconf');
var IndexModel = require('../models/index');
module.exports = function (app) {
var model = new IndexModel();
//or attach it directly to the app object like so
app.set('adventurer', nconf.get('adventurer'));
console.log('adventurer directly set on app object', app.get('adventurer'));
console.log('controller with app adventurer:', nconf.get('adventurer'));
app.get('/', function (req, res) {
res.render('index', model);
});
};
Fire it up with npm start and watch the console. Peace!

How to create extendable controllers in ExpressJS

I'm new to Node and I'm trying to create an MVC app with ExpressJS (http://expressjs.com/). I'm using the same folder structure as the MVC example (https://github.com/visionmedia/express/tree/master/examples/mvc) on GitHub.
In my controllers folder, I have 2 folders: main and system. What I'd like is to have a base controller defined in /controllers/system/index.js and have /controllers/main/index.js inherit the system controller. Every other module will extend system and override a few functions to generate a page.
In another tutorial I found the following code.
Base.js
var _ = require("underscore");
module.exports = {
name: "base",
extend: function(child) {
return _.extend({}, this, child);
},
run: function(req, res, next) {
}
};
Home.js
var BaseController = require("./Base"),
View = require("../views/Base"),
model = new (require("../models/ContentModel"));
module.exports = BaseController.extend({
name: "Home",
content: null,
run: function(req, res, next) {
model.setDB(req.db);
var self = this;
this.getContent(function() {
var v = new View(res, 'home');
v.render(self.content);
})
},
getContent: function(callback) {
var self = this;
this.content = {};
model.getlist(function(err, records) {
if(records.length > 0) {
self.content.bannerTitle = records[0].title;
self.content.bannerText = records[0].text;
}
model.getlist(function(err, records) {
var blogArticles = '';
if(records.length > 0) {
var to = records.length < 5 ? records.length : 4;
for(var i=0; i<to; i++) {
var record = records[i];
blogArticles += '\
<div class="item">\
<img src="' + record.picture + '" alt="" />\
' + record.title + '\
</div>\
';
}
}
self.content.blogArticles = blogArticles;
callback();
}, { type: 'blog' });
}, { type: 'home' });
}
});
How do you do this without Underscore's extend function? Does Express have a built in method to extend modules? I'm using doT.js for templating so I don't want to include another large library for one function.
Thanks!
Edit: Had to make a few changes to get the Base code from dc5 to work. System works but for main I'm getting this error on the inherits call:
util.js:555
ctor.prototype = Object.create(superCtor.prototype, {
^
TypeError: Object prototype may only be an Object or null
at Function.create (native)
at Object.exports.inherits (util.js:555:27)
/controllers/system/index.js:
var util = require( 'util' ),
system = { };
system.index = function( req, res, next ) {
res.render( 'main' );
};
module.exports = system;
/controllers/main/index.js:
var util = require( 'util' ),
system = require( './../system/index' ),
main = { };
util.inherits( main, system );
module.exports = main;
You can use util.inherits for what you've described. It isn't an replacement for _.extend(), but all that is needed for the example above is straight inheritance.
Usage: util.inherits(constructor, superConstructor)
base.js:
var util = require('util');
function Base() {…}
Base.prototype.run = function(req,res,next) {…}
module.exports = Base;
home.js:
var util = require('util');
var BaseController = require("./base");
function Home() {…}
util.inherits(home, BaseController);
Home.prototype.run = function(req,res,next) {…}
Or a standard JS inheritance pattern:
base.js:
function Base() {…}
Base.prototype.run = function(req,res,next) {…}
module.exports = Base;
home.js:
var BaseController = require("./base");
function Home() {
BaseController.apply(this, arguments);
}
Home.prototype = Object.create(BaseController.prototype);
Home.prototype.run = function(req,res,next) {…}
From the updated samples in the question, the modules should look like this:
System:
var util = require('util');
function System() {
this.txt = "hello from ";
this.name = "System";
}
System.prototype.sayHello = function() {
console.log(this.txt + this.name);
}
System.prototype.run = function(req,res,next) {
this.sayHello();
console.log('calling ==> overrideMe');
this.overrideMe();
console.log('calling ==> noOverride');
this.noOverride();
next ? next() : "";
}
System.prototype.overrideMe = function() {
console.log('System.overrideMe');
}
System.prototype.noOverride = function() {
console.log('System.noOverride');
}
module.exports = System;
Main:
var util = require('util');
var System = require("../system/");
function Main() {
// Makes sure the System constructor is run
System.apply(this, arguments);
this.name = "Main";
}
util.inherits(Main, System);
Main.prototype.run = function(req,res,next) {
this.sayHello();
console.log('calling ==> overrideMe');
this.overrideMe();
console.log('calling ==> noOverride');
this.noOverride();
next ? next() : "";
}
Main.prototype.overrideMe = function() {
console.log('Main.overrideMe');
}
module.exports = Main;
app.js in the root - a simplified express server:
var System = require('./controllers/system');
var Main = require('./controllers/main');
var express = require('express');
var path = require('path');
var http = require('http');
var app = express();
var server;
var system = new System();
var main = new Main();
app.configure(function() {
"use strict";
app.set('port', process.env.PORT || 3000, '127.0.0.1');
app.use(system.run.bind(system));
app.use(main.run.bind(main));
app.use(app.router);
//app.use(express.compress());
app.use(express.static(path.join(__dirname, '..', 'public'), {redirect: false}));
app.use(express.static(path.join("/Users/dave/personal/playground/yo"), {redirect: false}));
});
server = http.createServer(app);
server.listen(app.get('port'), function() {
"use strict";
console.log("Express server listening on port " + app.get('port'));
});
From the browser access: http://localhost:3000/
Console output:
Express server listening on port 3000
hello from System
calling ==> overrideMe
System.overrideMe
calling ==> noOverride
System.noOverride
hello from Main
calling ==> overrideMe
Main.overrideMe
calling ==> noOverride
System.noOverride
Important
Because this example is using instances of System and Main to provide routes, the run method must be bound to the instance when passing it to express. See: MDN .bind documentation for more information.
To avoid complicated configuration and workarounds, use ZinkyJS, it gives a better approach to make modules inheritance possible.

Categories