代码之家  ›  专栏  ›  技术社区  ›  ItsPronounced Finn

RESTAPI修补程序一次只能有一个字段

  •  1
  • ItsPronounced Finn  · 技术社区  · 6 年前

    我正在开发一个内联网格编辑器,它在网格中的单个值被更新后调用一个expressrestapi。因此,当用户更改网格中的单个字段时,我调用 PATCH

    // Update record based on TxnID
    router.patch('/editablerecords/update', function (req, res) {
        let qb_TxnID = req.body.txnid
        let type = req.body.type;
        let margin = req.body.margin;
    
    
    
        if (!qb_TxnID) {
            return res.status(400).send({ error:true, message: 'Please provide TxnID' });
        }
    
        connection.query("UPDATE pxeQuoteToClose SET ? WHERE qb_TxnID = '" + qb_TxnID + "'", { type, margin  }, function (error, results, fields) {
            if(error){
                res.send(JSON.stringify({"status": 500, "error": error, "response": null })); 
                //If there is error, we send the error in the error section with 500 status
            } else {
                res.send(JSON.stringify({ error: false, data: results, message: 'Record updated.' }));
                //If there is no error, all is good and response is 200OK.
            }
        });
    });
    

    我一次也只更新一个字段 type margin connection.query() 方法,但找不到任何信息,我也不明白它是如何构建查询的,除了 req.body.value 传递给它的用于生成查询的。

    编辑:我想添加,我可能想更新两个字段,但我也希望一次更新一个字段。谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   Eric Stein    6 年前

    the RFC PATCH call不应该是更新的表示,而是应用于资源的一组指令。

    PATCH方法请求 由媒体类型标识的文档。

    补丁 https://tools.ietf.org/html/rfc6902

       [
         { "op": "test", "path": "/a/b/c", "value": "foo" },
         { "op": "remove", "path": "/a/b/c" },
         { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
         { "op": "replace", "path": "/a/b/c", "value": 42 },
         { "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
         { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
       ]