Variable does not return desired object - javascript

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);
}

Related

Next.js grpc-node usage

I use gRPC but I have a problem initializing the service in Next.js app.
Goal: Create client service only once in app and use it in getServerSideProps (app doesn't use client-side routing).
For example, we have a service generated with grpc-tools (only available on SSR) and then I just want to initialize it somewhere. At first I thought it can be realized in a custom server.js:
const { credentials } = require('#grpc/grpc-js');
const express = require("express");
const next = require("next");
const { MyserviceClient } = require('./gen/myservice_grpc_pb');
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
// Init & Export
exports.myService = new MyserviceClient(
'http://localhost:3000',
credentials.createInsecure(),
);
(async () => {
await app.prepare();
const server = express();
server.get("*", (req, res) => handle(req, res));
server.listen(process.env.PORT, () => {
console.log(`Listening at http://localhost:${process.env.PORT}`);
});
})();
And then use it on the homepage, for example:
import React from 'react';
const { GetSmthRequest } = require('../gen/myservice_pb');
const { myService } = require('../server.js');
const IndexPage = () => (
<div>
<span>My HomePage</span>
</div>
)
const getServerSideProps = async () => {
const request = new GetSmthRequest();
request.setSomeStuff('random');
myService.getStmh(GetSmthRequest, (err, res) => {
//...
})
return {
props: {
}
}
}
export default IndexPage;
But for some reason it's not possible to initialize the client service in the server.js.
Also I tried doing it with next.config.js:
const { credentials } = require('#grpc/grpc-js');
const { MyserviceClient } = require('./gen/myservice_grpc_pb');
module.exports = {
serverRuntimeConfig: {
myService: new MyserviceClient(
'http://localhost:3000',
credentials.createInsecure(),
),
},
};
This solution works, so I can use the service through serverRuntimeConfig, thereby initializing it only once in the entire application, but when I make a request somewhere using getServerSideProps, I get an error:
Request message serialization failure: Expected argument of type ...
Error explanation: (https://stackoverflow.com/a/50845069/9464680)
That error message indicates that message serialization
(transformation of the message object passed to gRPC into binary data)
failed. This generally happens because the message object doesn't
match the expected message type or is otherwise invalid
Does anyone know why I am getting this error?
It's also interesting to see some examples of using Next.js with grpc-node.
For such a case you can use Node.js global

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();

Problem with adding imported function into exported function

So basically I want this block:
const {someFunction} = require("/somePath");
exports.exportedFunction = (req, res) => { someFunction() }
to work like this block:
exports.exportedFunction = (req, res) => {
const {someFunction} = require("/somePath");
someFunction();
}
But without repeating the imports. How to prevent repeating imports into every single export? I want to have one import from which I can use the function into every export without importing exactly into exports. Is that even possible?
UPDATE: okay, there's small update. I have made a minimum requirement problem recreation and it looks like this:
//index.js
const express = require("express");
const app = express();
const router = require("./router");
exports.exportableStuff = () => {
return [1,2,3];
};
app.use("/", router);
app.listen(3000, () => {
console.log("i am listening");
});
//router.js
const express = require("express");
const router = express.Router();
const controller = require("./controller");
router.get("/", controller.testModule);
module.exports = router;
//controller.js
const {exportableStuff} = require("./index");
exports.testModule = (req, res) => {
console.log("it has been done");
exportableStuff();
res.send("<h1>hello, user</h1>");
}
UPDATE2: I actually managed to fix the closures myself. What I actually did to make this work is changing this:
const {exportableStuff} = require("./index");
Into:
const model = require("./index");
And call the function in my controller with model.exportableStuff().
Problem oficially solved, but if you have better idea, I'm all ears.
According to your comment, I think you might want to do following thing?
// index.js
module.exports = { someFunction }
// router.js
// case1
const { someFunction } = require("./index.js")
someFunction() // it will work
// case2
const index = require("./index.js")
index.someFunction() // it will work
But, if you want to export your someFunction to another exports
then use new function, you need to do like this.
// another.js
const { someFunction } = require("./index.js")
exports.exportedFunction = someFunction
// router.js
const { exportedFunction } = require("./another.js")
exportedFunction() // it will work

Unit Testing Controllers use Jest, NodeJS

I want to check a case that certain routes are calling the correct controller use Jest specific (mock or spy).
It is case specific for unit testing. Somebody can help me how to check it use jest. I don't need verify kind of
expect (status code or res object) i need to check if controller have been called.
Thanks!
For instance:
// todoController.js
function todoController (req, res) {
res.send('Hello i am todo controller')
}
// index.spec.js
const express = require('express');
const request = require('request-promise');
const todoController = require('./todoController');
jest.mock('./todoController');
const app = express();
app.get('/todo', todoController)
test('If certain routes are calling the correct controller , controller should to have been called times one.', async() => {
await request({url: 'http://127.0.0.1/todo'})
expect(todoController).toHaveBeenCalledTimes(1);
})
Actually if you search, there are many references out there.
In the following, I share a few ways that I know.
One of the big conceptual leaps to testing Express applications with mocked request/response is understanding how to mock a chained
API eg. res.status(200).json({ foo: 'bar' }).
First you can make some kind of interceptor, this is achieved by returning the res instance from each of its methods:
// util/interceptor.js
module.exports = {
mockRequest: () => {
const req = {}
req.body = jest.fn().mockReturnValue(req)
req.params = jest.fn().mockReturnValue(req)
return req
},
mockResponse: () => {
const res = {}
res.send = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
return res
},
// mockNext: () => jest.fn()
}
The Express user-land API is based around middleware. AN middleware that takes a request (usually called req), a response (usually called res ) and a next (call next middleware) as parameters.
And then you have controller like this :
// todoController.js
function todoController (req, res) {
if (!req.params.id) {
return res.status(404).json({ message: 'Not Found' });
}
res.send('Hello i am todo controller')
}
They are consumed by being “mounted” on an Express application (app) instance (in app.js):
// app.js
const express = require('express');
const app = express();
const todoController = require('./todoController');
app.get('/todo', todoController);
Using the mockRequest and mockResponse we’ve defined before, then we’ll asume that res.send() is called with the right payload ({ data }).
So on your test file :
// todo.spec.js
const { mockRequest, mockResponse } = require('util/interceptor')
const controller = require('todoController.js')
describe("Check method \'todoController\' ", () => {
test('should 200 and return correct value', async () => {
let req = mockRequest();
req.params.id = 1;
const res = mockResponse();
await controller.todoController(req, res);
expect(res.send).toHaveBeenCalledTimes(1)
expect(res.send.mock.calls.length).toBe(1);
expect(res.send).toHaveBeenCalledWith('Hello i am todo controller');
});
test('should 404 and return correct value', async () => {
let req = mockRequest();
req.params.id = null;
const res = mockResponse();
await controller.todoController(req, res);
expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: 'Not Found' });
});
});
This is only 1 approach to testing Express handlers and middleware. The alternative is to fire up the Express server.

"Home" is not a Constructor on Node JS

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(...)

Categories