我的API响应成功块为什么不执行我的函数调用?

huangapple go评论67阅读模式
英文:

Is there a reason why my API response success block is not executing my function calls?

问题

"Relatively new to web development and I am currently taking a course which builds a face recognition application using React and Clarifai API. The application has a search box where the user pastes an image URL, which is then supplied to the Clarifai API. I have created a function called 'calculateFaceLocation' that should accept the API response and perform an action on. My issue is that I am receiving the required response from the Clarifai API (Which I can confirm by console logging 'response' in my success block) but when I call 'this.calculateFaceLocation(response)' it appears that my function is not being executed. It seems like it has to be something trivial that I am missing but any help would be appreciated. Just to note I have removed my personal API key from the below code block"

import React, { Component } from 'react';
import ParticlesBG from './components/Particles/Particles.js';
import Navigation from './components/Navigation/Navigation.js';
import Logo from './components/Logo/Logo.js';
import Rank from './components/Rank/Rank.js';
import ImageLinkForm from './components/ImageLinkForm/ImageLinkForm.js';
import FaceRecognition from './components/FaceRecognition/FaceRecognition.js';
import Clarifai from 'clarifai';

window.process = {
    env: {
        NODE_ENV: 'development'
    }
}

const app = new Clarifai.App({
  apiKey: "YOUR_API_KEY"
});

class App extends Component {
  constructor() {
    super()
    this.state = {
      input: "",
      imageURL:"",
      box:{}
    }
  }

  calculateFaceLocation = (data) => {
    console.log(data);
  };

  onInputChange = (event) => {
    this.setState({input: event.target.value});
  };

  onButtonClick = () => {
    this.setState({imageURL: this.state.input});
    app.models.predict({ 
                 id: 'face-detection', 
                 name: 'face-detection', 
                 version: '6dc7e46bc9124c5c8824be4822abe105', 
                 type: 'visual-detector', 
             },
             this.state.input
            )
    .then(
      (response) => {
        this.calculateFaceLocation(response);
      },
      (err) => {
        console.log(err);
      }
    );
  };

  render() {
    return (
      //My JSX code is in here but not required to show.
    );
  }
}

export default App;

Tried to console.log(response) in my response success block which worked and confirmed response was received. However, any function I attempt to run within the success block appears not to execute.

英文:

Relatively new to web development and I am currently taking a course which builds a face recognition application using React and Clarifai API. The application has a search box where the user pastes an image URL, which is then supplied to the Clarifai API. I have created a function called 'calculateFaceLocation' that should accept the API response and perform an action on. My issue is that I am receiving the required response from the Clarifai API (Which I can confirm by console logging 'response' in my success block) but when I call 'this.calculateFaceLocation(response)' it appears that my function is not being excecuted. I seems like it has to be something trivial that I am missing but any help would be appreciated. Just to note I have removed my personal API key from the below code block

import React, { Component } from 'react';
import ParticlesBG from './components/Particles/Particles.js';
import Navigation from './components/Navigation/Navigation.js';
import Logo from './components/Logo/Logo.js';
import Rank from './components/Rank/Rank.js';
import ImageLinkForm from './components/ImageLinkForm/ImageLinkForm.js';
import FaceRecognition from './components/FaceRecognition/FaceRecognition.js';
import Clarifai from 'clarifai';
window.process = {
env: {
NODE_ENV: 'development'
}
}
const app = new Clarifai.App({
apiKey: " "
});
class App extends Component {
constructor() {
super()
this.state = {
input: "",
imageURL:"",
box:{}
}
}
calculateFaceLocation = (data) => {
console.log(data);
};
onInputChange = (event) => {
this.setState({input: event.target.value});
};
onButtonClick = () => {
this.setState({imageURL: this.state.input});
app.models.predict({ 
id: 'face-detection', 
name: 'face-detection', 
version: '6dc7e46bc9124c5c8824be4822abe105', 
type: 'visual-detector', 
},
this.state.input
)
.then(
function(response) {
this.calculateFaceLocation(response);
},
function(err) {
console.log(err);
}
);
};
render() {
return (
//My JSX code is in here but not required to show.
}
export default App;

Tried to console.log(response) in my response success block which worked and confirmed response was received. However, any function I attemt to run withing the success block appears not to excecute.

答案1

得分: 0

问题在于您的回调函数是内联函数,像这样包装的:

function(response) {
  this.calculateFaceLocation(response);
}

在此上下文中的this是函数内部的上下文(而不是类实例),将内联函数替换为箭头函数,以使this成为类的实例:

(response) => {
   this.calculateFaceLocation(response);
}

您可能会收到以下错误:

Uncaught SyntaxError: Unexpected identifier 'calculateFaceLocation'
英文:

The issue is that your then callbacks are wrapping as inline functions like this:

function(response) {
this.calculateFaceLocation(response);
}

this in this context is the context inside the function (not the class instance), replace your inline functions with arrow functions, to have this be the instance of the class:

(response) => {
this.calculateFaceLocation(response);
}

You are probably receiving the following error:

Uncaught SyntaxError: Unexpected identifier 'calculateFaceLocation'

huangapple
  • 本文由 发表于 2023年2月24日 03:48:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定