2 yavispuu yavispuu 于 2014.06.12 02:41 提问

本人刚入门js,请问像如下的这段代码大致该怎么重构

var prepareCart = function (host, accessToken, username, state, count,filename) {
if (count == 0) {
completeTest(state);
}
else {
log("INFO", "Preparing the cart");
request.post({
url: host + '/ls/api/testing/randomItems',
headers: {
'Authorization': 'Bearer ' + accessToken
},
form: {
"type": "Product",
"count": 10
}
},
function (error, postResponse, body) {
//console.log(body);
var randomProducts = JSON.parse(body);
request.post({
url: host + '/ls/api/testing/randomItems',
headers: {
'Authorization': 'Bearer ' + accessToken
},
form: {
"type": "Account",
"count": 1
}
},
function (error, postResponse, body) {
//console.log(body);
var randomAccount = JSON.parse(body)[0];
//Now we can create a cart
var cart = {}
cart["headers"] = {
category: "work", type: "Cart",
clientId: "TEST-" + uuid.v1(),
revisionId: uuid.v1(),
action: "Create"
}
cart["data"] = {
cartId: cart.headers.clientId,
accountId: randomAccount["data"]["accountId"],
name: cart.headers.clientId,
processStatus: "/Workflow/processStatus[Verify]",
processType: "/Workflow/cartType[Order]/processType[1]",
userId: username,
combineForInvoice: true,
lineItems: []
}
var lineItemId = 0;
randomProducts.forEach(function (product, index) {
if (product == null) {
return;
}
var productId = product.data.productId;
var productDescription = product.data.productDescription;
var uom = product.data.baseUOM;
lineItem = {
lineItemId: lineItemId,
productId: productId,
uom: uom,
productDescription: productDescription,
requestedQuantity: 10
}
cart.data.lineItems.push(lineItem);
lineItemId = lineItemId + 1;
});
})
});
}

Csdn user default icon
上传中...
上传图片
插入图片