如何访问使用javascript从函数返回的对象的属性?

如何访问使用javascript从函数返回的对象的属性?,javascript,Javascript,我有一个返回对象的方法。如何检索该对象的属性 下面是我的代码 const called_method = async (context, user) => { const available = await context.dbHandler({ sql: 'SELECT SUM(first) AS first, SUM(second) AS second FROM items;', values: { id: user[

我有一个返回对象的方法。如何检索该对象的属性

下面是我的代码

const called_method = async (context, user) => {
    const available = await context.dbHandler({
        sql:
           'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
           values: { id: user[0].comp_id },
    });
    return available[0];
};


const some_method = () => {
    const available = called_method(context, user);
    const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
   }
}
如何从被调用的方法返回first和second,并在某个_方法中访问它


苏蒙能帮我做这个吗。谢谢。

异步函数总是返回一个承诺,因此,如果你想得到它的值,你必须等待它解决。您还可以将某些_方法设置为异步函数:

const some_method = async () => {
    const available = await called_method(context, user); // wait until the promise settles
    const data = {
        first: available.first, 
        second: available.second
   }
}

异步函数总是返回一个承诺,因此如果您想要得到它的值,您必须等待它解决。您还可以将某些_方法设置为异步函数:

const some_method = async () => {
    const available = await called_method(context, user); // wait until the promise settles
    const data = {
        first: available.first, 
        second: available.second
   }
}

要与承诺的最终价值互动,您需要使用它的。然后方法:

或者,您需要将代码放在异步函数中,等待承诺

const some_method = async () => {
  const available = await called_method(context, user);
  const data = {
     first: available.first,
     second: available.second,
  }
}

要与承诺的最终价值互动,您需要使用它的。然后方法:

或者,您需要将代码放在异步函数中,等待承诺

const some_method = async () => {
  const available = await called_method(context, user);
  const data = {
     first: available.first,
     second: available.second,
  }
}

您必须等待异步函数:

const called_method = async (context, user) => {
const available = await context.dbHandler({
    sql:
       'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
       values: { id: user[0].comp_id },
});
return available[0];
};


const some_method = () => {
    called_method(context, user).then(function(available) {
      const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
      }
  }
}
或者,您也可以使您的某些方法异步,并等待被调用的\u方法:

const called_method = async (context, user) => {
const available = await context.dbHandler({
    sql:
       'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
       values: { id: user[0].comp_id },
});
return available[0];
};


const some_method = async () => {
    const available = await called_method(context, user);
    const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
    }
}

您必须等待异步函数:

const called_method = async (context, user) => {
const available = await context.dbHandler({
    sql:
       'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
       values: { id: user[0].comp_id },
});
return available[0];
};


const some_method = () => {
    called_method(context, user).then(function(available) {
      const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
      }
  }
}
或者,您也可以使您的某些方法异步,并等待被调用的\u方法:

const called_method = async (context, user) => {
const available = await context.dbHandler({
    sql:
       'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
       values: { id: user[0].comp_id },
});
return available[0];
};


const some_method = async () => {
    const available = await called_method(context, user);
    const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
    }
}
被调用的_方法是一个异步函数,它返回一个。你必须用它才能得到你需要的东西。了解更多信息。所谓的_方法是一个异步函数,它返回一个。你必须用它才能得到你需要的东西。阅读更多关于。