Javascript 为什么我在提交表单时出现500错误?
我想知道为什么我在尝试将照片上传到DB时出现500错误。我感觉我的控制器和我的React代码中的axios调用都出了问题。巴斯德宾在下面。如果你需要更多的信息,请告诉我 这里是App.jsJavascript 为什么我在提交表单时出现500错误?,javascript,php,reactjs,laravel,debugging,Javascript,Php,Reactjs,Laravel,Debugging,我想知道为什么我在尝试将照片上传到DB时出现500错误。我感觉我的控制器和我的React代码中的axios调用都出了问题。巴斯德宾在下面。如果你需要更多的信息,请告诉我 这里是App.js import React, {Component} from 'react'; import axios from 'axios'; import Feed from '../components/Feed/Feed'; import Upload from '../components/Upload/Upl
import React, {Component} from 'react';
import axios from 'axios';
import Feed from '../components/Feed/Feed';
import Upload from '../components/Upload/Upload';
import ImagePreview from './ImagePreview/ImagePreview';
class App extends Component {
constructor(props) {
super(props);
this.state = {
selectedFile: null,
previewImgURL: '',
imgPrev: false,
success: false,
progress: 0,
imageChosen: false,
pictures: [],
hideForm: true,
};
this.imageUpload = this.imageUpload.bind(this);
this.submitImageAndRedirect = this.submitImageAndRedirect.bind(this);
this.postIsClicked = this.postIsClicked.bind(this);
this.feedView = this.feedView.bind(this);
}
imagePreview(newPostImageBool) {
this.setState({imgPrev: newPostImageBool});
if (this.state.selectedFile === null) {
alert("can't preview a picture on this because it's empty");
this.setState({imgPrev: false});
}
};
closeModal() {
this.setState({imgPrev: false});
};
imageUpload(e) {
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = () => {
this.setState({
selectedFile: file,
previewImgURL: reader.result,
pictures: [reader.result]
}, () => {
console.log(this.state.pictures);
})
};
if (file) reader.readAsDataURL(file); // Allows user to preview image uploaded
this.setState(() => ({file}));
this.setState({success: true, imageChosen: true});
}
submitImageAndRedirect() {
// e.preventDefault();
let picUrl = this.state.previewImgURL;
axios.post('/home', {
body: picUrl
}).then(response => {
// console
console.log(response);
// set state
this.setState({
pictures: [picUrl, response.data]
});
});
console.log("submitImageAndRedirect() triggered");
}
postIsClicked(e) {
console.log("postIsClicked(e) triggered");
if (e.target.value === "Yes") {
this.feedView();
this.submitImageAndRedirect();
console.log(`Yes has been clicked... inside Yes if block`);
} else {
alert("No clicked");
}
}
feedView() {
this.setState({hideForm: false}, () => console.log(this.state.hideForm));
}
render() {
return (
<div className="feed-wrapper">
{this.state.success ?
<div className="alert alert-success">
<strong>Chosen image is successful!
Now click Preview and make sure that's the one you want to upload!</strong>
</div> : null}
{this.state.hideForm ?
<form onSubmit={this.submitImageAndRedirect}>
<div className="inputWrapper">
<input
id="new_post_image"
name="post_image"
className="button is-success is-outlined"
type="file"
style={{display: 'none'}}
onChange={this.imageUpload}
accept="image/*"
/>
<Upload/>
<br/>
{
this.state.imageChosen ?
<div className="uploaded-pics">
<ImagePreview src={this.state.previewImgURL} onClick={this.postIsClicked}/>
</div> : null
}
</div>
</form>
: null
}
{!this.state.hideForm ?
this.state.pictures.map(post => {
return <Feed src={post} />
})
:null}
</div>
);
}
}
export default App;
Laravel日志中的错误:
[2019-10-06 16:25:56] local.ERROR: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mywebsite.post_pictures' doesn't exist (SQL: select * from `post_pictures` where `post_pictures`.`user_id` = 5 and `post_pictures`.`user_id` is not null) {"userId":5,"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mywebsite.post_pictures' doesn't exist (SQL: select * from `post_pictures` where `post_pictures`.`user_id` = 5 and `post_pictures`.`user_id` is not null) at /Users/garenvartanian/workstation/mywebsite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mywebsite.post_pictures' doesn't exist at /Users/garenvartanian/workstation/mywebsite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:326)
[stacktrace]
该错误表示,laravel无法在mywebsite数据库中找到您的表post_图片 你创建了这个表吗 如果没有,请为make表创建迁移: 有时您可能希望使模型名与数据库上的表名不同。 您可能需要向模型添加表名,如:
希望能有所帮助。谢谢错误是这样说的,laravel在我的网站数据库中找不到你的表post_图片 你创建了这个表吗 如果没有,请为make表创建迁移: 有时您可能希望使模型名与数据库上的表名不同。 您可能需要向模型添加表名,如:
希望能有所帮助。谢谢阅读错误消息。@IGP只是发布了错误,最初没有发布。我很抱歉。你认为发到“/home”的帖子会导致服务器执行你在问题中提出的create方法吗?@Bravo我有点纠结于此,不知道如何回避这个问题。我需要发出post请求,因为我想在DB中发布一些东西。粘贴错误的堆栈跟踪。转到您的日志:storage/logs/laravel-xxxx.log读取错误消息。@IGP只是发布了错误,最初没有发布。我很抱歉。你认为发到“/home”的帖子会导致服务器执行你在问题中提出的create方法吗?@Bravo我有点纠结于此,不知道如何回避这个问题。我需要发出post请求,因为我想在DB中发布一些东西。粘贴错误的堆栈跟踪。转到您的日志:storage/logs/laravel-xxxx.log
POST http://mywebsite.test/home 500 (Internal Server Error)
[2019-10-06 16:25:56] local.ERROR: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mywebsite.post_pictures' doesn't exist (SQL: select * from `post_pictures` where `post_pictures`.`user_id` = 5 and `post_pictures`.`user_id` is not null) {"userId":5,"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mywebsite.post_pictures' doesn't exist (SQL: select * from `post_pictures` where `post_pictures`.`user_id` = 5 and `post_pictures`.`user_id` is not null) at /Users/garenvartanian/workstation/mywebsite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mywebsite.post_pictures' doesn't exist at /Users/garenvartanian/workstation/mywebsite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:326)
[stacktrace]
class Flight extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'my_flights';
}