I am currently working with Firebase real-time database and Firebase cloud functions (using Typescript). Below is the structure of my database nodes:
Orders Node:
orders
|- {Push ID}
|--billing_id
|--orders_id
Shippings Node:
shippings
|- {Push ID}
|-- billing_id
|-- user_id
My function monitors changes on the "orders" node and retrieves the value of the "billing_id".
Note: The billing_id matches the one stored in the "shippings" node. This billing_id will always exist in the "shippings" node.
Next, I aim to target the shippings node that contains the exact billing_id and fetch the user_id. Below is a shortened version of the code:
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
//I need assistance from this point onwards.
let db = admin.database();
let val1:any;
db.ref(`/shippings/{pushId}/${billing_id}`).once("value") //What should I do next?
});
Edit 1:
I tried the solution recommended by Neelavar. Here are the warnings and errors I encountered while deploying the function:
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
NOTE: Please inform me if you require any other information. Also, provide a comprehensive solution as I am new to both Firebase and Typescript.