Jithin Zacharia
Jithin Zacharia

Reputation: 33

The app.locals is coming as undefined in Express.js

I am setting the app.locals in server.js here is my code

 //server.js
 app.locals.abi=abi;
 app.locals.SampleContract=SampleContract;
 app.locals.web3=web3;

But when I am calling the same from another file, app.locals is coming as undefined.

function getDetails(req,res,next){
    contract=req.locals.SampleContract;
    web3=req.locals.web3;
    contractAddress=req.locals.contractAddress;
    next();
}

Upvotes: 1

Views: 1415

Answers (2)

G'ofur N
G'ofur N

Reputation: 2651

Use req.app.locals not req.locals

Upvotes: 2

Sangram Badi
Sangram Badi

Reputation: 4274

set as key value pair

app.set('abi',abi);
app.set('SampleContract',SampleContract);
app.set('web3',web3);

get using key

function getDetails(req,res,next){
    contract=req.app.get('SampleContract');
    web3=req.locals.web3;
    contractAddress=req.locals.contractAddress;
    next();
}

Upvotes: 0

Related Questions