如何使用Java脚本
改变对象在Azure的功能,格式我有一个蓝色的功能,它负责调用“someStoreprocedure”,并通过REQ为对象。如何使用Java脚本
此相同REQ插入到了蔚蓝的收集
在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;
}
});
};
回答:
例如:
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'
}
]
}
回答:
我找到了解决我的问题:
下面是完整的代码: -
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;
}
});
}
以上是 如何使用Java脚本 的全部内容, 来源链接: utcz.com/qa/261305.html