Javascript 在null上调用成员函数move()(Rest api laravel)
我正在使用expo在react native中开发应用程序,后端在Laravel 基本上我在做个人资料模块来上传照片 开发了后端Api 这是我的后端代码Javascript 在null上调用成员函数move()(Rest api laravel),javascript,php,reactjs,laravel,react-native,Javascript,Php,Reactjs,Laravel,React Native,我正在使用expo在react native中开发应用程序,后端在Laravel 基本上我在做个人资料模块来上传照片 开发了后端Api 这是我的后端代码 public function EditProfile(Request $request){ //Creating model and input values $profile = new ProfilePicEditModel; $profile->user_id = Aut
public function EditProfile(Request $request){
//Creating model and input values
$profile = new ProfilePicEditModel;
$profile->user_id = Auth::id();
$profile->name = $request->name;
$profile->occupation = $request->occupation;
$profile->waystatus = $request->waystatus;
//Image Proceesing to Upload
$fileName=Auth::id().".png";
$path = $request->file('image')->move(public_path("/ShareYourMealProfilePics"),$fileName);
$photoURL= url('/ShareYourMealProfilePics/'.$fileName);
$profile->image=$photoURL;
$profile->phone = $request->phone;
$profile->save();
return response()->json($profile,200);
}
这是我的模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ProfilePicEditModel extends Model
{
protected $table="profile";
//public $timestamps=false;
protected $fillable=[
'user_id',
'name',
'occupation',
'waystatus',
'image',
'phone',
];
}
看起来文件没有上传。我不知道react,但在laravel中,您可以通过验证或使用条件if($request->hasFile('image')){//移动文件并保存url}
>来避免异常,还有一个问题,我们只存储图像的“uri”。我过去只在DB中保存配置文件中的文件名和路径,因此如果我将文件移动到其他路径,我可以轻松更改所有文件的路径。如果我尝试使用post man,它工作正常,图像的url上载到DB中,图像上载到公用文件夹中,但当使用react native时,则会出现错误,问题在于react代码。其他有经验的人可以帮你。尝试在问题中添加react和javascript标记,以接触更多人
export default class EditProfile extends React.Component{
constructor() {
super();
this.state = {
name:'',
occupation:'',
waystatus:'',
image:'',
phone:'',
};
}
selectPicture = async () => {
await Permissions.askAsync(Permissions.CAMERA_ROLL);
const { cancelled, uri } = await ImagePicker.launchImageLibraryAsync({
aspect: 1,
allowsEditing: true,
});
if (!cancelled) this.setState({ image: uri });
};
updateProfile=async()=>{
// console.log(this.state.name,this.state.occupation,this.state.waystatus,this.state.image,this.state.phone);
fetch('http://192.168.1.7:8000/api/EditProfile',{
method:'post',
headers:{
'Authorization': `Bearer ${GLOBAL.mytoken}`,
'Content-Type':'application/json',
'Accept': 'application/json'
},
body:JSON.stringify({
"name":this.state.name,
"occupation":this.state.occupation,
"waystatus":this.state.waystatus,
"image":this.state.image,
"phone":this.state.phone
})
}).then((response)=> response.json())
.then((res)=>{
if(typeof(res.message)!="undefined"){
Alert.alert(res.message);
}
else{
Alert.alert("Success","You have succesfuly Updated Your Profile",
[
{
text: 'Continue', onPress: () => {
Actions.Profile();
}
}
],
{ cancelable: false })
}
}).catch((error)=>{
console.error(error);
});
}
render(){
const {goBack} = this.props.navigation;
return(
<SafeAreaView style={styles.container}>
<ScrollView showsVerticalScrollIndicator={true} style={{marginHorizontal:10}}>
<View style={styles.titleBar}>
<Ionicons name="ios-arrow-back" size={24} color="#52575D"
onPress={() => goBack()}
></Ionicons>
<Text style={{fontSize: 22,fontWeight:"400",justifyContent:"center",marginLeft:110}}>Edit Profile </Text>
</View>
<Text style={{marginLeft:30,fontSize:17,marginTop:25,fontWeight:"300"}}>Please Select Your Profile Pic</Text>
<TouchableOpacity
onPress={this.selectPicture}>
<FontAwesome name="camera" size={35} style={{justifyContent:"center",marginTop:17,alignSelf:"center"}}/>
</TouchableOpacity>
<Item floatingLabel style={{marginTop:10}}>
<Label style={{marginLeft:30}}>Name </Label>
<Input style={{marginLeft:10,marginRight:10}}
onChangeText={(name) => this.setState({name})}
/>
</Item>
<Item floatingLabel style={{marginTop:20}}>
<Label style={{marginLeft:30}}>Sub Name/Occupation </Label>
<Input style={{marginLeft:10,marginRight:10}}
onChangeText={(occupation) => this.setState({occupation})}
/>
</Item>
<Item floatingLabel style={{marginTop:20}}>
<Label style={{marginLeft:30}}>Status </Label>
<Input style={{marginLeft:10,marginRight:10}}
onChangeText={(waystatus) => this.setState({waystatus})}
/>
</Item>
<Item floatingLabel style={{marginTop:20}}>
<Label style={{marginLeft:30}}>Phone No.</Label>
<Input style={{marginLeft:10,marginRight:10}} keyboardType={'numeric'} returnKeyType='done'
onChangeText={(phone) => this.setState({phone})}
/>
</Item>
<Button rounded style={styles.postSeat}
onPress={()=>this.updateProfile()}>
<Text>Update</Text>
</Button>
</ScrollView>
</SafeAreaView>
)}
}