gpt4 book ai didi

javascript - Syncano Codebox - 调用 API - 解析 JSON - 获取引用 - 保存新对象

转载 作者:数据小太阳 更新时间:2023-10-29 05:25:25 25 4
gpt4 key购买 nike

我将 Syncano 用作 baas,我试图调用外部 API 来接收 JSON 数组。这个 JSON 需要被解析,然后存储在 syncano 中。在此之前,我需要从数据库接收引用对象以将其链接到新的团队对象。

我成功收到团队 (json) 数组和引用对象。但是我无法存储新数据,因为只有 12-14 个团队(必须是 18 个)被保存。

我用 promise 尝试了这个和那个,但没有成功。任何人都可以建议如何重写代码以存储所有数据?谢谢 - 这是我目前所拥有的...

//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);
});

关于javascript - Syncano Codebox - 调用 API - 解析 JSON - 获取引用 - 保存新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35563152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com