我尝试使用从另一个文件导入的此代码调用APP,并且效果很好:
-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEAxM4QOYgTiyJgcBv5zg2qZjpLINt6fmub3JbIVmFaHKeC8Xvp
但是现在我想将我的userId从1更改为Asyncstorage的常量,因此我决定将代码更改为:
import FormData from 'FormData';
import AsyncStorage from '@react-native-community/async-storage';
let formData = new FormData();
formData.append('userId', '1'); // < this is what I want to change
formData.append('key', '***'); //my key
export function getScoreFromAPI () {
return fetch('https://www.globalfidelio.com/gfn_arcol/api/transaction.php',{
method : 'POST',
headers : {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
body : formData
} )
.then((response) => {
return response.json()
})
.catch((error) => console.log("l'erreure est: " + error))
}
具有try-catch函数,但是当我在 constructor(props) {
super(props)
this.state = { infos: [], userId: '' }
}
componentWillMount() {
this.getScoreFromAPI().then(data => {
this.setState({ infos: data })
});
console.log(this.state.infos);
AsyncStorage.getItem(USERID_STORED)
.then((data) => {
if (data) {
this.setState({userId:data})
}
});
}
async getScoreFromAPI() {
let formData = new FormData();
formData.append('userId', this.state.userId);
formData.append('key', '***'); //my key
try {
let response = await fetch('https://www.globalfidelio.com/gfn_arcol/api/transaction.php',{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
body: formData
})
let res = await response.json();
} catch(error) {
console.warn("errors are " + error);
}
};
中调用getScoreFromAPI()
时,无法用接收到的数据来设置状态,信息中仍然有一个空数组:[]
我的问题:
如何用第一个文件中asyncstorage中的值替换userId中的'1'?
如果不可能,我该如何处理setState信息:[]并显示我的数据
答案 0 :(得分:1)
public void CreateDefaultRolesAndUsers()
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
IdentityRole role = new IdentityRole();
if(!roleManager.RoleExists("Admins"))
{
role.Name = "Admins";
roleManager.Create(role);
ApplicationUser user = new ApplicationUser();
user.UserName = "Ahmed";
user.Email = "ahmed@live.com";
var Check = userManager.Create(user, "Ahmed*90");
if(Check.Succeeded)
{
userManager.AddToRole(user.Id, "Admins");
}
}
}
不返回任何内容,这就是您的getScoreFromAPI
无法正常工作的原因。setState
和try
,promise具有它们自己的错误处理机制(.catch()方法)。catch
相比,我认为回调更易读,并且错误更少。这就是我要做的:
.then()
答案 1 :(得分:1)
我已将您的代码简化为一个Promise链,在从AsyncStorage获得getScoreFromAPI
之后,将执行调用userId
,然后将响应存储到infos
状态,同时返回{ {1}}(如果有错误),并将错误记录到控制台。先前未从null
返回数据,因此该值将始终变为getScoreFromAPI
。我尚未测试此代码,但这将为您提供一个良好的工作基础:
null
答案 2 :(得分:0)
终于完成了。我尝试了一下,它奏效了。谢谢大家
这就是我所做的:
...
const USERID_STORED = "userid_stored";
const GSM_STORED = "gsm_stored";
...
class ScoreList extends React.Component {
constructor(props) {
super(props)
this.state = { infos: [], userId: '', gsmStored: '', }
}
componentWillMount() {
AsyncStorage.getItem(USERID_STORED)
.then(userId => {
this.setState({ userId: userId});
this.getScoreFromAPI(this.state.userId).then(data => {
this.setState({ infos: data });
});
});
AsyncStorage.getItem(GSM_STORED)
.then(gsmStore => {
this.setState({ gsmStored: gsmStore});
});
}
getScoreFromAPI (userId) {
let formData = new FormData();
formData.append('userId', userId);
formData.append('key', '***');
return fetch('https://***',{
method : 'POST',
headers : {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
body : formData
} )
.then((response) => {
return response.json()
})
.catch((error) => console.log("l'erreure est: " + error))
};