flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

Populate not working #161

Closed amirhemm closed 3 years ago

amirhemm commented 3 years ago

I'm using the 1.0.0-alpha.34 and when I add relational data through add() method I have to manually save the record manually on Flamelink panel before I can use populate to get the data. When I use the add() method I just save the ID. Is this the write way of doing it? Or I would have to get a reference and save the reference?

gitdubz commented 3 years ago

Hi @amirhemm

Can you please share the code for adding the item?

amirhemm commented 3 years ago

Hi @gitdubz,

Here is the bit of code that I am using to add the item: await flamelinkApp.content.add({ schemaKey: 'orders', data: { email: session.customer.email, customer: customerId, dateCreated: convertISODatetoLocalTime(new Date()), shippingAddress, paymentIntent: session.payment_intent, paymentStatus: session.payment_status, orderStatus: 'processing', uid: session.metadata.uid, orderDetails: { item, shippingCost:$${(session.total_details.amount_shipping / 100).toFixed(2)}, shippingCostNum: session.total_details.amount_shipping, salesTaxAmount:$${(session.total_details.amount_tax / 100).toFixed(2)}, salesTaxAmountNum: session.total_details.amount_tax, discountAmount:$${(session.total_details.amount_discount / 100).toFixed(2)}, discountAmountNum: session.total_details.amount_discount, subtotal:$${(session.amount_subtotal / 100).toFixed(2)}, subtotalNum: session.amount_subtotal, total:$${(session.amount_total / 100).toFixed(2)}, totalNum: session.amount_total, }, total:$${(session.amount_total / 100).toFixed(2)}, orderNotes: [] } })

In this schema the customer is a reference field and also under orderDetails.item I have a product field which is a reference field to the products. The data is added just fine. I believe my problem maybe that I would have to get the reference to customer and product and the document reference instead of just adding the ID directly. Can you please hit me to the right direction. Thanks

update:

I have used the firebase admin SDK to create a docRef and it is working fine now. Is there a way to create a reference with flamlink sdk?

gitdubz commented 3 years ago

Hi @amirhemm

There is no method currently to generate a reference for Cloud Firestore.

For the benefit of others, to generate a reference

With the use of Cloud Firestore

// entryId would be the document ID (can be found on _fl_meta_.docId of an entry if needed)

// content reference
const ref = firebase.firestore().doc(`fl_content/${entryId}`)

// navigation reference
const ref = firebase.firestore().doc(`fl_navigation/${entryId}`)

// user reference
const ref = firebase.firestore().doc(`fl_users/${entryId}`)

// file reference
const ref =  firebase.firestore().doc(`fl_files/${entryId}`)

With the use of Realtime DB (please note that a reference should not be passed for RTDB when adding content only the entryId)

// for "single" type schemas
const ref = flamelinkApp.content.ref(schemaKey)

// for "collection" type schemas
const ref = flamelinkApp.content.ref([schemaKey, entryId])
amirhemm commented 3 years ago

@gitdubz Thanks for the great explanations. Just to expand on that the same is possible with the admin SDK incase if anyone is trying to make a ref serverside.