Home > database >  How to return properties of a response object along with a new object?
How to return properties of a response object along with a new object?

Time:10-22

So I am making a POST request like so:

async createVisit(patientId: string, visitorId: string) {
  const userJWT = jwt.sign(
    {
      _id: visit,
      refreshCount: 0
    },
    this.localConfig.spirit.jwtSecret
  )
  const visitURL = this.localConfig.spirit.url
  let response = await fetch(visitURL, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer'   userJWT
    },
    body: JSON.stringify(visit)
  });
  if (response.ok) {
   let result = await response.json();
  }
}

So I need to return couple of properties out of that result along with a new object to access inside the method of another class.

CodePudding user response:

You can only return 1 thing from a function, but if you do need to return 2 things, you can combine them in an array

return [coupleOfPropertiesFromResult, newObject];

You can also use an object.

...
let result = await response.json();

return { 
  prop: result.prop,
  another: result.another,
  yetanother: 42 
}
  • Related