Typescript 使用Firebase云函数读取特定节点的同级数据
我正在使用Firebase实时数据库和Firebase云函数(在Typescript中)。 以下是我的数据库节点的结构: 订单节点:Typescript 使用Firebase云函数读取特定节点的同级数据,typescript,firebase,firebase-realtime-database,google-cloud-functions,Typescript,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在使用Firebase实时数据库和Firebase云函数(在Typescript中)。 以下是我的数据库节点的结构: 订单节点: orders |- {Push ID} |--billing_id |--orders_id 发货节点: shippings |- {Push ID} |-- billing_id |-- user_id 我的函数观察“订单”节点上的更改并获取“账单id”的值 注意:此账单id与存储在中的账单id相同 “发货”节点。此
orders
|- {Push ID}
|--billing_id
|--orders_id
发货节点:
shippings
|- {Push ID}
|-- billing_id
|-- user_id
我的函数观察“订单”节点上的更改并获取“账单id”的值
注意:此账单id与存储在中的账单id相同
“发货”节点。此账单id将始终显示在“发货”节点中
在下一步中,我希望以包含准确账单id的shippings节点为目标,并获取用户id。
以下是代码(最短版本):
编辑1:
尝试了尼拉瓦尔建议的解决方案。下面是我在部署函数时收到的警告和错误
WARNING: A:/HN/functions/src/index.ts:79:9 - Forbidden 'var' keyword, use 'let' or 'const' instead
WARNING: A:/HN/functions/src/index.ts:79:13 - Identifier 'db' is never reassigned; use 'const' instead of 'var'.
ERROR: A:/HN/functions/src/index.ts:86:24 - Shadowed name: 'snapshot'
WARNING: A:/HN/functions/src/index.ts:149:9 - Forbidden 'var' keyword, use 'let' or 'const' instead
WARNING: A:/HN/functions/src/index.ts:149:13 - Identifier 'options' is never reassigned; use 'const' instead of 'var'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\RIYASREE'S_PC\AppData\Roaming\npm-cache\_logs\2020-10-15T14_18_07_047Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
注意:如果需要任何其他信息,请告诉我。
另外,请提供详细的解决方案,因为我对Firebase和Typescript都是新手
由于您已经拥有来自
onCreate
触发器的billing\u id
,现在您需要查询shippings
节点以获取已知的billing\u id
db.ref(`/shippings`)
.orderbyChild('billing_id')
.equalTo(billing_id).once("value", snapshot => {
// Now use the snapshot to access the shippings collection document
// which now has both billing_id and user_id - only with matching billing_id
});
在Neelavar的帮助和稍微调整代码后,我成功地获取了所需的值。下面是运行代码:
export const orderEmail = functions.database.ref('/orders/{pushId}')
.onCreate((snapshot,context)=>{
const original = snapshot.val(); //Stores values from the newly created orders node
const billing_id = original.billing_id; // Stores the billing_id from the newly created orders node
var db = admin.database();
var val1:any;
return db.ref(`/shippings`)
.orderbyChild('billing_id')
.equalTo(billing_id).once("value", snap:any => {
const data = snap.val();
const pushId = Object.keys(data)[0]; //Gets the Push Id
const userId = data[pushId].user_id; // Gets the user_id
console.log("User Id", userId)
});
});
嘿我有错误。你能再查一下吗?我正在编辑带有错误消息的问题。如果启用了typescript严格检查,请使用
.once(“值”,(快照:any)=>{
export const orderEmail = functions.database.ref('/orders/{pushId}')
.onCreate((snapshot,context)=>{
const original = snapshot.val(); //Stores values from the newly created orders node
const billing_id = original.billing_id; // Stores the billing_id from the newly created orders node
var db = admin.database();
var val1:any;
return db.ref(`/shippings`)
.orderbyChild('billing_id')
.equalTo(billing_id).once("value", snap:any => {
const data = snap.val();
const pushId = Object.keys(data)[0]; //Gets the Push Id
const userId = data[pushId].user_id; // Gets the user_id
console.log("User Id", userId)
});
});