React应用报错TypeError: Cannot read properties of undefined (reading '0')求助
TypeError: Cannot read properties of undefined (reading '0') in SmartBrain App after Knex update
问题背景
- 1个月前部署了Udemy《Zero to Mastery》课程的SmartBrain项目:前端托管在GitHub,后端及数据库部署在Heroku
- 此前运行完全正常,6天前检查无任何问题
- 10小时前收到Heroku邮件提示后端knex 2.3.0存在安全漏洞,需升级至2.4.0;升级后出现报错,降级回knex 2.3.0问题依旧,甚至课程导师的代码也出现相同错误
应用对接Clarifai人脸检测API,报错信息如下:
Error: TypeError: Cannot read properties of undefined (reading '0') at App.calcFaceLocation (App.js:47:1) at App.js:92:1
相关代码(App.js)
import { Component } from 'react'; import Navigation from './Components/Navigation/Navigation'; import SignIn from './Components/SignIn/SignIn'; import Register from './Components/Register/Register'; import Logo from './Components/Logo/Logo' import ImgLinkForm from './Components/ImgLinkForm/ImgLinkForm'; import FaceRecognition from './Components/FaceRecognition/FaceRecognition'; import Rank from './Components/Rank/Rank'; import Particles from './Components/Particles/Particles'; import './App.css'; const initialState = { input: '', imageUrl:'', box:{}, route:'signin', isSignedIn: false, user: { id: '', name: '', email: '', entries: 0, joined: '' } } class App extends Component { constructor(){ super(); this.state = initialState; } loadUser = (data) => { this.setState({user: { id: data.id, name: data.name, email: data.email, entries: data.entries, joined: data.joined }}) } calcFaceLocation = (data) => { const clarifaiFace = data.outputs[0].data.regions[0].region_info.bounding_box; const image = document.getElementById('inputimage'); const width = Number(image.width); const height = Number(image.height); return { leftCol: clarifaiFace.left_col * width, topRow: clarifaiFace.top_row * height, rightCol: width - (clarifaiFace.right_col * width), bottomRow: height - (clarifaiFace.bottom_row * height) } } dispFaceBox = (box) => { this.setState({box: box}) } onInputChange = (e) => { this.setState({input: e.target.value}) } onPictureSubmit = () => { this.setState({imageUrl: this.state.input}); fetch('https://smart-brain-api-db.herokuapp.com/imageurl', { method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ input: this.state.input }) }) .then(response => response.json()) .then(response => { if(response) { fetch('https://smart-brain-api-db.herokuapp.com/image',{ method: 'put', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ id: this.state.user.id }) }) .then(response => response.json()) .then(count => { this.setState(Object.assign(this.state.user, { entries: count })) }) .catch(console.log) } this.dispFaceBox(this.calcFaceLocation(response)) }) .catch(err => console.log(err)); } onRouteChange = (route) => { if (route === 'signout') { this.setState(initialState) } else if (route === 'home') { this.setState({isSignedIn: true}) } this.setState({route: route}) } render() { const { isSignedIn, imageUrl, route, box } = this.state; return ( <div className="App"> <div className="particles"> <Particles /> </div> <Navigation isSignedIn={isSignedIn} onRouteChange={this.onRouteChange}/> { route === 'home' ? <div> <Logo /> <Rank name={this.state.user.name} entries={this.state.user.entries}/> <ImgLinkForm onInputChange={this.onInputChange} onPictureSubmit={this.onPictureSubmit}/> <FaceRecognition box={box} imageUrl={imageUrl}/> </div> : ( route === 'signin' ? <SignIn loadUser={this.loadUser} onRouteChange={this.onRouteChange}/> : <Register loadUser={this.loadUser} onRouteChange={this.onRouteChange} /> ) } </div> ); } } export default App;
已在谷歌搜索各类解决方案达6小时,未找到类似案例,如需更多文件可告知。
内容的提问来源于stack exchange,提问作者Grzesiek9002
相关产品推荐
相关产品推荐

