如何使用Javascript更改Azure函数中的对象格式

问题描述 投票:0回答:2

我有一个azure函数,负责调用“someStoreprocedure”并将req作为对象传递。

这个相同的req被插入到azure集合中

req中的数据如下

`{
    "intObjectName" : "JCI-AOMS-SWM",
    "aomsOrderReleaseNbr" : "7232046001",
    "shipToFaxNbr" : "7325609699",
    "50records" : [ {
        "aomsLineNbr" : 1,
        "planShipPtShipDate" : "20170101",
        "product" : {
            "name" : "test-product-train",
            "productDisplayName" : "test-product-train-display",
            "sku" : "TRAIN-SKU",
        },
        "licenses" : [ {
            "productKey" : "productKey-1",
            "status" : "not activated"
        }, {
            "productKey" : "productKey-2",
            "status" : "not activated"
        },

        {
            "productKey" : "productKey-3",
            "status" : "not activated"
        } ]
    } ],
    "isEntitlementInProgress" : true,
    "id" : "1dcf296e-4e2f-b3f5-8117-92a7879e0a9b"
}` 

我想将其更改为不同的格式,如下所示,只需要发送到存储过程插入即可。

`{
intObjectName: "JCI-AOMS-SWM"
productKeys: [
{
            "productKey" : "productKey-1",
            "status" : "not activated"
},
{
            "productKey" : "productKey-1",
            "status" : "not activated"
},
{
            "productKey" : "productKey-1",
            "status" : "not activated"
},
]
}` 

我的JS代码如下,

请告知我代码中的更正。

var DocumentDBClient = require('documentdb').DocumentClient;
module.exports = function(context, req) {
    var host = "some";
    var masterKey = "some=";
    var spName = "someStoreprocedure";
    var client = "";
    client = new DocumentDBClient(host, {
        masterKey : masterKey
    });

    var insertSPLink = "dbs/" + "admin" + "/colls/" + "productsoutput"
            + "/sprocs/" + spName;
    client.executeStoredProcedure(insertSPLink,req,function(err, res) {
                        if (err) {
                            return callback(statusObj);
                        } else {
                            context.log("Success in Insertion");
                            context.done();
                            return context;
                        }
                    });
};
javascript json node.js azure-functions
2个回答
0
投票

例如:

var req = {
    "intObjectName": "JCI-AOMS-SWM",
    "aomsOrderReleaseNbr" : "7232046001",
    "shipToFaxNbr" : "7325609699",
    "50records" : [ {
        "aomsLineNbr" : 1,
        "planShipPtShipDate" : "20170101",
        "product" : {
            "name" : "test-product-train",
            "productDisplayName" : "test-product-train-display",
            "sku" : "TRAIN-SKU",
        },
        "licenses" : [ {
            "productKey" : "productKey-1",
            "status" : "not activated"
        }, {
            "productKey" : "productKey-2",
            "status" : "not activated"
        },

        {
            "productKey" : "productKey-3",
            "status" : "not activated"
        } ]
    } ],
    "isEntitlementInProgress" : true,
    "id" : "1dcf296e-4e2f-b3f5-8117-92a7879e0a9b"
};

var formatedReq = {
    intObjectName: req["intObjectName"],
    productKeys: req["50records"][0]["licenses"]
}

console.log(formatedReq);

输出:

{
    intObjectName: 'JCI-AOMS-SWM',
    productKeys: [
        {
            productKey: 'productKey-1',
            status: 'not activated'
        },
        {
            productKey: 'productKey-2',
            status: 'not activated'
        },
        {
            productKey: 'productKey-3',
            status: 'not activated'
        }
    ]
}

0
投票

我找到了解决问题的方法:

以下是完整的代码: -

var DocumentDBClient = require('documentdb').DocumentClient;

var newRecordOutputObj;

module.exports = function(context, req) {

    context.log('In orderTiggerRecords');

    var host = "some";
    var masterKey = "some=";
    var spName = "insertRecords";
    var outputCollection = "records";
    var databaseName = "some";
    var client = "";
    client = new DocumentDBClient(host, {
        masterKey : masterKey
    });
    var storedProc = "dbs/" + databaseName + "/colls/" + outputCollection
            + "/sprocs/" + spName;

    newRecordOutputObj = req;
    context
            .log("====================Complete Order JSON==============================");
    context.log(JSON.stringify(newRecordOutputObj));

    var orderData = {};

    for (var i = 0; i < newRecordOutputObj[0]['50records'][0].licenses.length; i++) {
        orderData = orderData
                + {
                    productkey : newRecordOutputObj[0]['50records'][0].licenses[i].productKey,
                    status : newRecordOutputObj[0]['50records'][0].licenses[i].status

                };
    }

    context
            .log("==================== Inserting into Record Collection ====================");
    context.log(orderData);

    client
            .executeStoredProcedure(
                    storedProc,
                    orderData,
                    function(err, res) {
                        if (err) {
                            spName = "";
                            fetchSPLink = "";
                            return callback(statusObj);
                        } else {
                            context
                                    .log("=========================== Done ==============================");
                            context.res = {
                                status : 200,
                            };
                            context.done();
                            return context;
                        }
                    });
}
© www.soinside.com 2019 - 2024. All rights reserved.