【发布时间】:2019-05-31 08:45:01
【问题描述】:
我正在尝试使用 Firebase 的管理 SDK 获取第一个匹配子对象的对象。到目前为止,我能够在父对象中获取一个对象:
{ -LVOBaJXoai9n1mYrs3F: { created: 1546610959716, displayEnterpriseNumber: "BE 0479.312.137", email: "anthony.magnus@gmail.com", enterpriseNumber: "479312137" } }
但我想要
{ created: 1546610959716, email: "john.doe@gmail.com", token: '51234', isActive: true }
代码
在 firebase 函数中,我有以下代码:
try {
const token = req.body.token;
const couponSnapshot = await admin.database().ref(`coupons`).orderByChild('token').equalTo(token).limitToFirst(1).once('value');
const coupon = couponSnapshot.val();
console.log(coupon) //{-LVOBaJXoai9n1mYrs3F: {…}} Object in Object...
} catch(error) {
//error handling
}
如何从父对象中获取子对象?
我尝试遍历 couponSnapshot 并将子值附加到 coupon 变量,但出现错误。
const couponSnapshot = await admin.database().ref(`coupons`).orderByChild('token').equalTo(token).limitToFirst(1).once('value');
let coupon = {};
couponSnapshot.forEach(childSnapshot => {
coupon = childSnapshot.val();
});
console.log(coupon.email);
错误:
error TS2345: Argument of type '(childSnapshot: DataSnapshot) => void' is not assignable to parameter of type '(
a: DataSnapshot) => boolean'.
Type 'void' is not assignable to type 'boolean'.
Property 'email' does not exist on type '{}'.
有人可以帮帮我吗?
【问题讨论】:
标签: typescript firebase firebase-realtime-database google-cloud-functions