我如何返回一个承诺结果

时间:2021-07-14 09:03:01

标签: javascript reactjs

我是 ReactJS 和 Javascript 的新手,我在这里尝试做的是映射 userCart 结果。有一个类似于 product_postedby 的数据。然后我将使用 product_postedby 数据并在 firestore 数据库中搜索它,并得到名为 displayName 的结果但它向我显示了这个错误 Uncaught Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.

当我尝试使用 console.log(display) 而不是 return display 时,它显示的是这个而不是结果:

Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "NAME"

如何返回 PromiseResult 而不是 promise?

function test(name) {

   const display = db.collection('users').doc(name).get()
   .then((doc2) => {
   return doc2.data().displayName
   })

   return display   //I want to return the displayName data here
}

return(
<>

{userCart.map((cart, i) => (
  <div>
    {test(cart.products.product_postedby)}
  </div>
))}

</>

0 个答案:

没有答案