【发布时间】:2015-10-29 08:56:31
【问题描述】:
我有一个客户想要将 8000 件商品推送到他的 shopify 商店。
我编写了这段代码,但我遇到了一个问题:除非我将传出连接限制为大约 1-2 个,否则发布的项目会以未定义的方式响应并失败...。我的循环/发布请求可能太快,但所有方法我试图通过失败来减慢它的速度。
- 这里是他们设置的 API 限制
每秒 2 次呼叫,一次可爆发 40 次呼叫。
我正在使用 node.js 和 ms sql 插件。来自 ms sql 的数据通过流很好地到达并被推送到我的数组 rowPush,然后我循环通过它(8000)以通过单播发送 post req。
sql.connect(userConfig, function(err) {
if (err) {
console.log("you done screwed up the dang connection to SQL " + err)
};
var request = new sql.Request();
request.stream = true;
request.verbose = true;
request.query('SELECT intProductID, Stock, strPurDesc, Vendor, Brand, intPurchasePrice, strBarCode FROM V_ProductList ORDER BY intProductID');
var rowPush = [];
//row is the object that returns from MySQL database.
request.on('row', function(row) {
rowPush.push(row);
});
request.on('error', function(err) {
console.log('err occured ' + err);
});
request.on('done', function(returnValue) {
//my for loop for looping through every item in rowPush.
for (i =0; i < rowPush.length; i++ ) {
var newProduct = {
"product": {
"title": rowPush[i].strPurDesc,
"id": rowPush[i].intProductID,
"vendor": rowPush[i].Vendor,
"product_type": rowPush[i].Brand,
"variants": [
{
//"id": 1044399237,
//"product_id": 1071559589,
"inventory_management":"shopify",
"inventory_quantity": rowPush[i].Stock,
"barcode": rowPush[i].strBarCode,
"price": rowPush[i].intPurchasePrice,
"taxable" : true,
}
]
}
};
//console.log(JSON.stringify(newProduct));
var sendNewItem = function (){
unirest.post('https://5b848c9ca0e184f13140629d9c2d34ca:b8b14f71ad82a7d8f00520f8a4f5f571@teststoresrh.myshopify.com/admin/products.json')
//.header('Accept', 'application/json')
.set('Content-Type', 'application/json')
.send(newProduct)
.end(function (response) {
console.log(response.body);
});
}
if (rowPush[i].Vendor) {
sendNewItem();
};
//sendNewItem();
}
});
console.log(rowPush[8210]);
});
//});
sql.on('error', function(err) {
console.log("you done screwed up the dang connection to SQL " + err);
});
【问题讨论】:
-
您尝试了哪些方法来减慢速度? shopify 是否允许“捆绑”数据以便只有一个(或几个)帖子?有错误信息吗?
-
没有错误,如果我尝试一次发送两个以上的对象,只是未定义。当它工作时,我收到一个包含 vmy 上传响应的响应....看起来它不喜欢推送大数据集。
标签: javascript sql-server node.js stream shopify