0

I have a few js that only contain export const and those are not any class or function. I have an async-await function to fetch data from the database. Now I need to create another const that will only fetch the first data from the result. and there is another const where I need to set that data.

data fetching const in db.js

export const getLatestData = async () => {
  const url = `${testDB.serverUrl}/findLatestTen`;
  const res = await axios.get(url).then(res => res);
  return res;
};

I have another function that will retrieve only the first value

export const getFirst = async () => {
  const test = await getLatestData();
  const example = test.data[0].salary;
  // If I put console.log here I get the example value as expected
};

But I have another js file called employee.js where I have some const like this

export const details = [
  {
    title: 'HR',

    links: [
      {
        title: 'Employee 1',
        salary: async () => {
          await getFirst();
        }
      },
      {
        title: 'Employee 2',
        salary: async () => {
          await getFirst();
        }
      }
    ]
  }
];

I will use this employee.js to set value in the UI. But I am not getting the value. Can I please get some help?

TIA

1
  • should be salary: () => { getFirst(); }, now it just a promise, you need to resolve it. Commented Dec 30, 2020 at 14:16

1 Answer 1

0

await getFirst() will just run the function. If you want to get the you should return the function.

export const getFirst = async () => {
  const test = await getLatestData();
  const example = test.data[0].salary;
  return example; // return value
};
export const details = [
  {
    title: 'HR',

    links: [
      {
        title: 'Employee 1',
        salary: async () => {
          return await getFirst();  // return function result
        }
      },
      {
        title: 'Employee 2',
        salary: async () => {
          return await getFirst();  // return function result
        }
      }
    ]
  }
];
Sign up to request clarification or add additional context in comments.

8 Comments

I tried this but if I do this. salary: async () => { return await getFirst(); // return function result }. then autofix removes that return and I am still getting the error
If I do like this salary: async () => { const ex = await getFirst(); console.log(ex ${ex}); return ex; }. Then I am getting async () => { const ex = await Object(_pbfs__WEBPACK_IMPORTED_MODULE_0__["getFirst"])(); console.log(ex ${ex}); return ex; I am calling this pbf from a class and there I put a log
you'd better not export details array. Just define the array on where you're using it.
I can't change it, unfortunately. Because that's an existing structure. Basically, my goal is getFirst() should return a string and I want to use getFirst() may places
what is the exact error when you try to use like details[0].links[0].salary?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.