在Firebase云功能上实现承诺

时间:2019-02-22 06:10:18

标签: firebase firebase-realtime-database google-cloud-functions

我正在尝试从Cloud函数内部的Firebase数据库撤消价格节点,但是当我登录getPrice()函数时,它给了我Promise正在等待警告。我在这里想念什么吗?

get price Promise {
  <pending>,
  domain: 
   Domain {
     domain: null,
     _events: { error: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }

这是代码段

const price = getPrice();
console.log(getPrice());

function getPrice() {
    return db
        .ref(`/locations/${id}/price`)
        .once('value')
        .then(snapshot => {
            console.log('snapshot: ', snapshot.val());
            return snapshot.val();
        });
}

编辑 在promise上调用.then()时获取此日志消息

console.log(getPrice().then(data => data))
get price Promise {
  <pending>,
  domain: 
   Domain {
     domain: null,
     _events: { error: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }

1 个答案:

答案 0 :(得分:0)

使用异步/等待解决了该问题 Video on using async await in cloud functions

解决方案:

console.log('getDataFromNode', await getDataFromNode());

async function getDataFromNode() {
    const node = await db.ref(`/locations/${parkingId}/price
    const data = await node.once('value');
    return data.val();
}