Syncano Codebox - 调用 API - 解析 JSON - 获取引用 - 保存新对象
Syncano Codebox - Call API - parse JSON - get Reference - Save new Objects
我将 Syncano 用作 baas,我试图调用外部 API 来接收 JSON 数组。这个 JSON 需要被解析,然后存储在 syncano 中。在此之前,我需要从 DB 接收引用对象以 link 它到新的团队对象。
我成功收到团队 (json) 数组和引用对象。但是我无法存储新数据,因为只有 12-14 个团队(必须是 18 个)被保存。
我尝试了这个和那个,但没有成功。任何人都可以建议如何重写代码以存储所有数据?谢谢 - 这是我目前所拥有的...
//TODO: get from ARGS when executing this codebox
var teamKey = 394;
var requestURL = 'http://api.football-data.org/v1/soccerseasons/' + teamKey + "/teams";
var request = require("request");
var Syncano = require('syncano');
var Promise = require('bluebird');
var account = new Syncano({
accountKey: "abc"
});
var promises = [];
//from: http://docs.syncano.io/v1.0/docs/data-objects-filtering
//"_eq" means equals to
var filter = {
"query": {
"apikey": {
"_eq": apiKey
}
}
};
request({
headers: {
'X-Auth-Token': 'abc'
},
url: requestURL,
'Content-Type': 'application/json;charset=utf-8;',
method: 'GET',
}, function(error, response, body) {
if (error) {
console.log(error);
} else {
var json = JSON.parse(body);
var teamArray = json.teams;
var newObject;
account.instance('instance').class('competition').dataobject().list(filter)
.then(function(compRes) {
var competitionID = compRes.objects[0].id;
for (var i = 0; i < teamArray.length; i++) {
newObject = {
"name": teamArray[i].name,
"nameshort": teamArray[i].code,
"logo": teamArray[i].crestUrl,
"competition": competitionID
};
(account.instance('instance').class('teams').dataobject().add(newObject).then(function(res) {
console.log(res);
}).catch(function(err) {
console.log("Error eq: " + err);
})
);
}
}).catch(function(err) {
console.log(err);
});
}
});
问题可能是您在完成所有保存调用之前完成了请求过程,您可以尝试 Promise.all():
account.instance('instance').class('competition').dataobject().list(filter)
.then(function(compRes) {
var competitionID = compRes.objects[0].id, promises=[];
for (var i = 0; i < teamArray.length; i++) {
newObject = {
"name": teamArray[i].name,
"nameshort": teamArray[i].code,
"logo": teamArray[i].crestUrl,
"competition": competitionID
};
promises.push(account.instance('instance').class('teams').dataobject().add(newObject).then(function(res) {
console.log(res);
}).catch(function(err) {
console.log("Error eq: " + err);
})
);
}
return Promise.all(promises);
}).catch(function(err) {
console.log(err);
});
如果一次并行调用过多是问题,则将一个调用接一个调用:
account.instance('instance').class('competition').dataobject().list(filter)
.then(function(compRes) {
var competitionID = compRes.objects[0].id, promise = Promise.resolve();
function chainToPromise(promise, teamObj, waitTime){
waitTime = waitTime || 500;
return promise.then(function(){
return new Promise(function(resolve, reject){
setTimeout(resolve, waitTime);
});
}).then(function(){
return account.instance('instance').class('teams').dataobject().add(teamObj);
}).then(function(res) {
console.log(res);
}).catch(function(err) {
console.log("Error eq: " + err);
});
}
for (var i = 0; i < teamArray.length; i++) {
newObject = {
"name": teamArray[i].name,
"nameshort": teamArray[i].code,
"logo": teamArray[i].crestUrl,
"competition": competitionID
};
promise = chainToPromise(promise, newObject);
}
return promise;
}).catch(function(err) {
console.log(err);
});
我将 Syncano 用作 baas,我试图调用外部 API 来接收 JSON 数组。这个 JSON 需要被解析,然后存储在 syncano 中。在此之前,我需要从 DB 接收引用对象以 link 它到新的团队对象。
我成功收到团队 (json) 数组和引用对象。但是我无法存储新数据,因为只有 12-14 个团队(必须是 18 个)被保存。
我尝试了这个和那个,但没有成功。任何人都可以建议如何重写代码以存储所有数据?谢谢 - 这是我目前所拥有的...
//TODO: get from ARGS when executing this codebox
var teamKey = 394;
var requestURL = 'http://api.football-data.org/v1/soccerseasons/' + teamKey + "/teams";
var request = require("request");
var Syncano = require('syncano');
var Promise = require('bluebird');
var account = new Syncano({
accountKey: "abc"
});
var promises = [];
//from: http://docs.syncano.io/v1.0/docs/data-objects-filtering
//"_eq" means equals to
var filter = {
"query": {
"apikey": {
"_eq": apiKey
}
}
};
request({
headers: {
'X-Auth-Token': 'abc'
},
url: requestURL,
'Content-Type': 'application/json;charset=utf-8;',
method: 'GET',
}, function(error, response, body) {
if (error) {
console.log(error);
} else {
var json = JSON.parse(body);
var teamArray = json.teams;
var newObject;
account.instance('instance').class('competition').dataobject().list(filter)
.then(function(compRes) {
var competitionID = compRes.objects[0].id;
for (var i = 0; i < teamArray.length; i++) {
newObject = {
"name": teamArray[i].name,
"nameshort": teamArray[i].code,
"logo": teamArray[i].crestUrl,
"competition": competitionID
};
(account.instance('instance').class('teams').dataobject().add(newObject).then(function(res) {
console.log(res);
}).catch(function(err) {
console.log("Error eq: " + err);
})
);
}
}).catch(function(err) {
console.log(err);
});
}
});
问题可能是您在完成所有保存调用之前完成了请求过程,您可以尝试 Promise.all():
account.instance('instance').class('competition').dataobject().list(filter)
.then(function(compRes) {
var competitionID = compRes.objects[0].id, promises=[];
for (var i = 0; i < teamArray.length; i++) {
newObject = {
"name": teamArray[i].name,
"nameshort": teamArray[i].code,
"logo": teamArray[i].crestUrl,
"competition": competitionID
};
promises.push(account.instance('instance').class('teams').dataobject().add(newObject).then(function(res) {
console.log(res);
}).catch(function(err) {
console.log("Error eq: " + err);
})
);
}
return Promise.all(promises);
}).catch(function(err) {
console.log(err);
});
如果一次并行调用过多是问题,则将一个调用接一个调用:
account.instance('instance').class('competition').dataobject().list(filter)
.then(function(compRes) {
var competitionID = compRes.objects[0].id, promise = Promise.resolve();
function chainToPromise(promise, teamObj, waitTime){
waitTime = waitTime || 500;
return promise.then(function(){
return new Promise(function(resolve, reject){
setTimeout(resolve, waitTime);
});
}).then(function(){
return account.instance('instance').class('teams').dataobject().add(teamObj);
}).then(function(res) {
console.log(res);
}).catch(function(err) {
console.log("Error eq: " + err);
});
}
for (var i = 0; i < teamArray.length; i++) {
newObject = {
"name": teamArray[i].name,
"nameshort": teamArray[i].code,
"logo": teamArray[i].crestUrl,
"competition": competitionID
};
promise = chainToPromise(promise, newObject);
}
return promise;
}).catch(function(err) {
console.log(err);
});