简体   繁体   English

如何使用 javascript 访问从 function 返回的 object 的属性?

[英]How to access a property of an object which is returned from a function using javascript?

i have a method which returns an object.我有一个返回 object 的方法。 How do i retrieve the property of that object我如何检索该 object 的属性

below is my code,下面是我的代码,

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
   }
}

How can i return first and second from the called method and access it in some_method.我如何从被调用的方法中返回第一个和第二个并在 some_method 中访问它。

could soemone help me with this. soemone 可以帮我解决这个问题。 thanks.谢谢。

Async functions always return a promise, therefore if you want to get its value you have to wait until it settles.异步函数总是返回 promise,因此如果你想获得它的值,你必须等到它稳定下来。 You can set some_method as an asynchronous function as well:您也可以将some_method设置为异步 function :

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

To interact with the eventual value of a promise, you either need to use it's .then method:要与 promise 的最终值交互,您需要使用它的.then方法:

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

Or you need to put your code in an async function and await the promise或者您需要将代码放入async function 并await promise

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

You have to wait for the async function:您必须等待异步 function:

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
      }
  }
}

Or you can also make your some method async and await for called_method:或者你也可以让你的一些方法异步并等待被调用的方法:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM