sunny jindal
sunny jindal

Reputation: 101

ReferenceError: name is not defined

I have a code in Controller

const getLocalDatabaseResule = async (searchURL, reqBody) => {
commonCode(reqBody);
console.log(name); 

});

function commonCode(reqBody) {

 var name = reqBody.name;
 var phone= reqBody.phone;
 var email = reqBody.email;

}

Any idea how to add common function in controller

Upvotes: 0

Views: 346

Answers (2)

Viraj Doshi
Viraj Doshi

Reputation: 881

Also this is possible

const getLocalDatabaseResule = async (searchURL, reqBody) => {
var x = commonCode(reqBody);
console.log(x.name); 

});

function commonCode(reqBody) {

 this.name = reqBody.name;
 this.phone= reqBody.phone;
 this.email = reqBody.email;

}

Upvotes: 0

anttud
anttud

Reputation: 736

You need to return the body from commonCode

const getLocalDatabaseResule = async (searchURL, reqBody) => {
  const {name,phone,email} = commonCode(reqBody);
  console.log(name); 
};

function commonCode(reqBody) {

 const name = reqBody.name;
 const phone= reqBody.phone;
 const email = reqBody.email;
 return {name,phone,email}

}

getLocalDatabaseResule("searchURL", {name:"User",phone:"111", email:"[email protected]"})

Upvotes: 1

Related Questions