I'm rewriting a project of mine and was wondering how would I post an array of data where I reuse the return value of a previous post request as their ID. Here's a rough detail of the data structure

  • Checklist A
  • [ChecklistItem 1, ChecklistItem 2, ChecklistItem 3] has their ID set as Checklist A

So my current setup is I send Checklist A, get the return value from FaunaDB(which is its unique ID) then plug it in the array using array.map then resend the array to FaunaDB.

But i don't know how to save the array since the request paramater is already used up. so i was wondering what's the normal way to do this.

here's a code snippet of the function

app.post('/checklists', (req,res) =>{
    const checklist = {
        dateCreated: Date.now(),
        user: Call(Fn('getUser'),'10049'),
        equipmentid: 'PM160'
    };

    const _checklistItems = [{
        componentid: 'AIRLK',
        conditionid: 'OK',
        equipmentid: 'PM160',
        remarks: 'test'
    }]

    const ckdoc = client.query(
        Crt('checklists',checklist))
        .then((ret) => {
            //would like to catch this ret and plug it into _checklistitems as its ID
            //then send the _checklistitems to faunaDB
        });
        res.send(ckdoc);
});

function Crt(collection,data){
    return Create(
        Collection(collection),
        {data}
    )
}

UPDATE after @eskwayrd pointed out that you can chain client queries within a single express js request. i chained another client query where i save the checklist items collection along with the return reference from a previous query. though i had problems sending the it as an Array, saving it through array.map still worked.

app.post('/checklists', async (req,res) =>{
    const checklist = {
        dateCreated: Date.now(),
        user: Call(Fn('getUser'),'10049'),
        equipmentid: 'PM160'
    };

    const _checklistItems = [{
        componentid: 'AIRLK',
        conditionid: 'OK',
        equipmentid: 'PM160',
        remarks: 'test'
    }]

    var _ref;

    console.log(checklist)
    await client.query(
        Crt('checklists',checklist)        
    )
    .then((ret) => {
            _ref = ret.ref
    })

    _checklistItems.map(item => {
        item.checklist = _ref
        console.log(item)
        client.query(
            Crt('checklist_items', item)
        )
    })

});
1

There are 1 best solutions below

1
On

Using the Fauna JavaScript driver, the client object that you create is quite reusable; it is not "used up".

Generally, you can chain dependent queries like this:

client.query( ... your FQL query ... )
.then((result) => {
  const ref = result.ref
  client.query( ... another FQL query involving the ref ...)
  .then((result2) => {
    console.log(result2)
  })
})

Using async and await, you can avoid nesting with something like:

;(async () => {
const result = await client.query( ... FQL query 1 ...)
  .then((res) => res)
  .catch((err) => console.log(`query 1 failed: ${err}`))

const ref = result.ref
const result2 = await client.query( ... FQL query 2 ...)
  .then((res) => res)
  .catch((err) => console.log(`query 2 failed: ${err}`))
console.log(result2)
})()

Note that both examples are, effectively, equivalent, and also demonstrates how to extract a value from the reponse.