0

我想運行一個查詢實時數據庫在firebase https功能,但它不工作。這裏是我的代碼在firebase https功能查詢不起作用

exports.fetchPost = functions.https.onRequest((req, res) => { 
    if (req.method === 'PUT' || req.method === 'GET') { 
    res.status(403).send('Forbidden!') 
    } 
    let postId = JSON.stringify(req.query.postId) || 
    JSON.stringify(req.body.postId) 
    cors(req, res,() => { 
    admin.database().ref('items') 
     .child(postId) 
     .once('value', s => { 
      console.log(s.val(), s) 
      res.status(200).send(s.val()) 
     }) 
     .catch(e => res.status(500).send(e)) 
    }) 
}) 

我檢查了日誌。 s.val()返回null,而s返回一個不可用的firebase對象。

Firebase function log

另外,我正在上請求下列返回發送

Error response being returned from the request

任何幫助,將不勝感激,謝謝。

回答

0
admin.database().ref('items') 
    .once('value', data => data.forEach(s => { 
     s.key === postId ? console.log('success!') : console.log('not 
     found'); 
    })) 

試試這段只處理firebase的代碼。在這裏測試和工作,如果它不起作用,你能請你說明你的物品是如何儲存的?

+0

好的,會試試這個,謝謝你的迴應。我對firebase仍然陌生,現在一直在使用Parse服務器。我打算做的只是獲得單個項目。 –

+0

測試過它,沒有工作。與以前一樣的錯誤。另外你的代碼有錯誤,你沒有指定三元運算符的else部分。所以我用了一個if語句。 –

+0

是的,我完全忘了那裏的其他部分,我已經編輯了更清晰的答案,我希望它有幫助。 –