简体   繁体   English

使用 Node 中另一个发布数据的返回值发布数据的标准方法是什么 - Express / FaunaDB

[英]What's the standard way to post data using a return value from another post data in Node - Express / FaunaDB

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.我正在重写我的一个项目,并且想知道如何发布一个数据数组,在其中我重用以前发布请求的返回值作为他们的 ID。 Here's a rough detail of the data structure这是数据结构的粗略细节

  • Checklist A清单 A
  • [ChecklistItem 1, ChecklistItem 2, ChecklistItem 3] has their ID set as Checklist A [ChecklistItem 1, ChecklistItem 2, ChecklistItem 3] 的 ID 设置为 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.所以我当前的设置是我发送清单 A,从 FaunaDB 获取返回值(这是它的唯一 ID),然后使用 array.map 将其插入数组,然后将数组重新发送到 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这是 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. @eskwayrd之后的更新指出您可以在单个快速 js 请求中链接客户端查询。 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.虽然我在将它作为数组发送时遇到问题,但通过 array.map 保存它仍然有效。

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)
        )
    })

});

Using the Fauna JavaScript driver, the client object that you create is quite reusable;使用 Fauna JavaScript 驱动程序,您创建的客户端 object 可重复使用; 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:使用asyncawait ,您可以避免嵌套,例如:

;(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.请注意,这两个示例实际上是等效的,并且还演示了如何从响应中提取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM