在JSON中使用状态变量时无法获得所需的输出 import React,{Component}来自'React'; 进口{ 样式表, 看法 文本, 按钮 异步存储, 警觉的, 键盘 活动指示器, }从“反应本机”; 从“../constants/Color”导入颜色; 类UserPage_1扩展了组件{ logoutFunction=()=>{ AsyncStorage.removietem('user'); Alert.Alert(“成功记录日志”) this.props.navigation.navigate('LoginScreen'); } 建造师(道具){ 超级(道具) 此.state={ 用户标识:“”, 密码:“”, 孤岛加载:是的, 公司名称:'', } } componentDidMount(){ 键盘; AsyncStorage.getItem('userid')。然后((value)=>this.setState({userid:value})); AsyncStorage.getItem('password')。然后((value)=>this.setState({password:value})); 这是我的国家({ UserId:this.state.UserId, 密码:this.state.Password, }) const{UserId}=this.state; const{Password}=this.state; 取('http:///php/UserPage_1.php', { 方法:“POST”, 标题:{ “接受”:“应用程序/json”, “内容类型”:“应用程序/json”, }, 正文:JSON.stringify({ 用户id:UserId, 密码:密码, }) }).then((response)=>response.json()) .然后((responseJson)=>{ //如果服务器响应消息与数据匹配 if(responseJson!==null){ //然后打开配置文件活动并向配置文件活动发送用户电子邮件。 这是我的国家({ 孤岛加载:false, 公司名称:responseJson[0]['cmpny_name'], }) console.log(responseJson); } 否则{ this.setState({isLoading:false}); console.log(responseJson); } }).完成(); }
当我直接使用这些值时,responseJson输出是正确的 输出为:数组[对象{“cmpny_name”:“k”,“cus_name”:“k”,“id”:“1”,“mob_no”:“k”,“password”:“k”,“user_id”:“k”,},] 但是当我使用状态变量时,responsejson输出类似于:Object{“password”:“,”user_id:“,”}在JSON中使用状态变量时无法获得所需的输出 import React,{Component}来自'React'; 进口{ 样式表, 看法 文本, 按钮 异步存储, 警觉的, 键盘 活动指示器, }从“反应本机”; 从“../constants/Color”导入颜色; 类UserPage_1扩展了组件{ logoutFunction=()=>{ AsyncStorage.removietem('user'); Alert.Alert(“成功记录日志”) this.props.navigation.navigate('LoginScreen'); } 建造师(道具){ 超级(道具) 此.state={ 用户标识:“”, 密码:“”, 孤岛加载:是的, 公司名称:'', } } componentDidMount(){ 键盘; AsyncStorage.getItem('userid')。然后((value)=>this.setState({userid:value})); AsyncStorage.getItem('password')。然后((value)=>this.setState({password:value})); 这是我的国家({ UserId:this.state.UserId, 密码:this.state.Password, }) const{UserId}=this.state; const{Password}=this.state; 取('http:///php/UserPage_1.php', { 方法:“POST”, 标题:{ “接受”:“应用程序/json”, “内容类型”:“应用程序/json”, }, 正文:JSON.stringify({ 用户id:UserId, 密码:密码, }) }).then((response)=>response.json()) .然后((responseJson)=>{ //如果服务器响应消息与数据匹配 if(responseJson!==null){ //然后打开配置文件活动并向配置文件活动发送用户电子邮件。 这是我的国家({ 孤岛加载:false, 公司名称:responseJson[0]['cmpny_name'], }) console.log(responseJson); } 否则{ this.setState({isLoading:false}); console.log(responseJson); } }).完成(); },json,react-native,Json,React Native,当我直接使用这些值时,responseJson输出是正确的 输出为:数组[对象{“cmpny_name”:“k”,“cus_name”:“k”,“id”:“1”,“mob_no”:“k”,“password”:“k”,“user_id”:“k”,},] 但是当我使用状态变量时,responsejson输出类似于:Object{“password”:“,”user_id:“,”} 找不到原因?由于AsyncStorage返回一个Promise对象,您需要等待数据被提取出来 将logoutFunct
找不到原因?由于
AsyncStorage
返回一个Promise
对象,您需要等待数据被提取出来
将logoutFunction
设为async
函数,并等待数据从async存储中删除
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
Button,
AsyncStorage,
Alert,
Keyboard,
ActivityIndicator,
} from 'react-native';
import Color from '../constants/color';
class UserPage_1 extends Component {
logoutFunction = () => {
AsyncStorage.removeItem('user');
Alert.alert("Sucessfully LoggedOut")
this.props.navigation.navigate('LoginScreen');
}
constructor(props) {
super(props)
this.state = {
UserId: '',
Password: '',
isLoading: true,
CompanyName: '',
}
}
componentDidMount() {
Keyboard.dismiss();
AsyncStorage.getItem('userid').then((value) => this.setState({ UserId: value }));
AsyncStorage.getItem('password').then((value) => this.setState({ Password: value }));
this.setState({
UserId: this.state.UserId,
Password: this.state.Password,
})
const { UserId } = this.state;
const { Password } = this.state;
fetch('http://<myserverip>/php/UserPage_1.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: UserId,
password: Password,
})
}).then((response) => response.json())
.then((responseJson) => {
// If server response message same as Data Matched
if (responseJson !== null) {
//Then open Profile activity and send user email to profile activity.
this.setState({
isLoading: false,
CompanyName: responseJson[0]['cmpny_name'],
})
console.log(responseJson);
}
else {
this.setState({ isLoading: false });
console.log(responseJson);
}
}).done();
}
将您的componentDidMount
更改如下
logoutFunction = async () => {
await AsyncStorage.removeItem('user');
Alert.alert("Sucessfully LoggedOut")
this.props.navigation.navigate('LoginScreen');
}
异步组件didmount(){
键盘;
试一试{
const UserId=await AsyncStorage.getItem('UserId');
const Password=await AsyncStorage.getItem('Password');
if(UserId!==null&&Password!==null){
取('http:///php/UserPage_1.php', {
方法:“POST”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
},
正文:JSON.stringify({
用户id:UserId,
密码:密码,
})
}).then((response)=>response.json())
.然后((responseJson)=>{
if(responseJson!==null){
这是我的国家({
孤岛加载:false,
公司名称:responseJson[0]['cmpny_name'],
})
}
否则{
这是我的国家({
孤岛加载:false
});
}
}).完成();
}
}捕获(错误){
log(“检索数据时出错”);
}
}
因为我没有完整的源代码,所以我可以测试它。根据您的要求更改它。
不要怀疑。希望这能帮助你。为什么这个问题用PHP标记?我在后端使用PHP!当然,但问题似乎是关于JS,而不是PHP。标记应该是与问题直接相关的。Sry但是直接使用的是什么
值呢?代码有点太长,我建议下次发布一个简单的示例:)okay将注意到这一点!感谢您的回答,但此代码实际上使用数据(上一屏幕中的用户名和密码)获取用户详细信息并显示它们。那么为什么您要从AsyncStorage
获取用户id和密码?因为用户id和密码是在上一页(登录页)中输入的从该页面重定向到上述页面,异步存储用于创建本地存储session@KeerthieaaswarRamachandran它认为存储userId和Password有问题。如果可能,还可以共享您的登录代码。如果我在console.log中显示state变量,则传递的值显示正确,但当我将该值提供给cons时t变量并将其传递给php文件,该值为null
async componentDidMount() {
Keyboard.dismiss();
try {
const UserId = await AsyncStorage.getItem('userid');
const Password = await AsyncStorage.getItem('password');
if (UserId !== null && Password !== null) {
fetch('http://<myserverip>/php/UserPage_1.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: UserId,
password: Password,
})
}).then((response) => response.json())
.then((responseJson) => {
if (responseJson !== null) {
this.setState({
isLoading: false,
CompanyName: responseJson[0]['cmpny_name'],
})
}
else {
this.setState({
isLoading: false
});
}
}).done();
}
} catch (error) {
console.log('Error retrieving data');
}
}