I am using Clarifai API face detection and it is unable to fetch the URL which is provided from the constructor, rather than a variable which Clarifai provides in the default code
class App extends Component{
constructor(){
super();
this.state = {
input : '',
IMAGE_URL: '',
}
}
onInputChange = (event) =>{
this.setState({input: event.target.value});
}
onSubmit = () =>{
this.setState({IMAGE_URL : this.state.input});
const raw = JSON.stringify({
"user_app_id": {
"user_id": USER_ID,
"app_id": APP_ID
},
"inputs": [
{
"data": {
"image": {
"url": this.state.IMAGE_URL
}
}
}
]
});
const requestOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': 'Key ' + PAT
},
body: raw
};
// NOTE: MODEL_VERSION_ID is optional, you can also call prediction with the MODEL_ID only
// https://api.clarifai.com/v2/models/{YOUR_MODEL_ID}/outputs
// this will default to the latest version_id
fetch("https://api.clarifai.com/v2/models/" + MODEL_ID + "/versions/" + MODEL_VERSION_ID + "/outputs", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
I started facing this issue when I added IMAGE_URL in constructor to update it from my input form on the webpage. It works fine if i move IMAGE_URL out from the constructor and making it a variable and hard code the image url in the editor
Edit: This is the code after some tweaks. Still same error
onInputChange = (event) =>{
this.setState({input: event.target.value});
console.log(typeof(input),'TYPE OF INPUT');
var inp = this.state.input;
return inp
//console.log(inp);
console.log(typeof(inp)); //it is string here
}
onSubmit = () =>{
this.setState({IMAGE_URL : this.state.inp});
const raw = JSON.stringify({
"user_app_id": {
"user_id": USER_ID,
"app_id": APP_ID
},
"inputs": [
{
"data": {
"image": {
"url": this.state.IMAGE_URL
}
}
}
]
Edit 2: It's working now and I guess I broke some rules. I have declared a global variable and passed the value of the input field to it and then used it in my API.
var inp = ''; //THIS IS THE NEW VARIABLE
class App extends Component{
constructor(){
super();
this.state = {
input : '',
IMAGE_URL: '',
}
}
onInputChange = (event) =>{
this.setState({input: event.target.value});
inp = event.target.value;
console.log(inp);
return inp;
}
onSubmit = () =>{
console.log('*********',inp,'***********');
this.setState({IMAGE_URL : this.state.input});
const raw = JSON.stringify({
"user_app_id": {
"user_id": USER_ID,
"app_id": APP_ID
},
"inputs": [
{
"data": {
"image": {
"url": inp
}
}
}
]