0

我已经做 js 大约一个月了,我正在编写这个程序,我在其中使用 clarifai API 来查看照片上的人最像哪个名人。

我想将输出作为道具传递给 Rank 组件来渲染它,但我得到了

类型错误:clarifaiResults.map 不是 App.transformResponse 中的函数

基本上,我想作为道具传递的响应是 const clarifaiResults = response.outputs[0].data.regions[0].data.concepts[0].name; 我现在在 console.log 中得到的部分

我假设这是因为当应用程序尝试呈现组件时还没有输出,但我无法弄清楚代码有什么问题。谢谢!

应用程序.js

import React, { Component } from 'react';
import './App.css';
import SignIn from './Component/SignIn/SignIn.js';
import Register from './Component/Register/Register.js';
import Particles from 'react-particles-js';
import Logo from './Component/Logo/Logo.js';
import Navigation from './Component/Navi/Navigation.js';
import ImageLinkForm from './Component/Form/ImageLinkForm.js';
import Rank from './Component/Rank/Rank.js'
import Clarifai from 'clarifai';
import FaceRecognition from './Component/Face/FaceRecognition.js';
import FaceComparison from './Component/Comparison/FaceComparison.js';

const app = new Clarifai.App({
  apiKey: 'MYSUPERSECRETKEY'
});

const initialState = {
  input: "",
  imageUrl: "",
  results: [],
  route: "SignIn",
  user: {
    id: "",
    name: "",
    email: "",
    entries: 0,
    joined: "",
  },
};

const particleOptions = {
  particles: {
    number: {
      value: 40,
      density: {
        enable: true,
        value_area: 800,
      },

    }
      }
    }

class App extends Component{
  constructor() {
    super();
    this.state = initialState;
  }

    transformResponse = (response) => {
      const clarifaiResults = response.outputs[0].data.regions[0].data.concepts[0].name;
    
      const results = clarifaiResults.map((ingredient) => ({
        ingredients: ingredient.name,
        probablitiy: ingredient.value,
      }));

      this.setState({results: results.celebrityName});
    
      return {results: []};
    };

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

  }

  onSubmit = () => {
    this.setState({imageUrl: this.state.input});
    app.models
    .predict(
      Clarifai.CELEBRITY_MODEL,
      this.state.input)
      .then(response => {
        console.log(response.outputs[0].data.regions[0].data.concepts[0].name)
        if (response) {
          fetch ('http://loclhost:3000', {
            method: 'post',
            headers: {'Conent-Type' : 'application/json'},
            body: JSON.stringify({
              input: this.state.user.input
            })
          })
          .then((response) => response.json())
          .then(count => {
            this.setState(Object.assign(this.state.user, {entries:count}))
          })
        }
        this.transformResponse(response);
      })
      .catch(err => console.log(err));
        

      };  
         
      ;



    onRouteChange = (route) => {
      if (route === 'signout'){
        this.setState({isSignedIn: false})
      } else if (route ==='home'){
        this.setState({isSignedIn: true})
      }
      this.setState({route: route});
    }

  render() { 
    let { isSignedIn, imageUrl, route, results} = this.state;
    return (
      <div className="App">
            <Particles className='particles'
             params={particleOptions} 
             />
            <Navigation isSignedIn={isSignedIn} onRouteChange={this.onRouteChange}/>
            { route ==='home'  
              ? <div>
                  <Logo />
                  <Rank 
                   results = {results}/>
                  <ImageLinkForm 
                    onInputChange={this.onInputChange} 
                    onSubmit={this.onSubmit}
                    />
                    <FaceRecognition 
                    imageUrl={imageUrl}    
                  />
                  <FaceComparison 
                    results = {results}
                  />
                </div>
              : (
                    route === 'SignIn' 
                  ? <SignIn onRouteChange={this.onRouteChange}/>
                  : <Register />
              )
               
            }
      </div>
    );
  };
}

export default App;

Rank.js

    import React from 'react';

const Rank = ({results}) => {
const prediction = results.map((result) => {
const {ingredients}  = result;
return (
    <div>
        <li className="celebrityName">{ingredients}</li>
    </div>
);
});

if (prediction && prediction.length>1) {
    return (
         <div>
            <div className='white f3'>
                You look a lot like...
                </div>
            <div className='white f1'>
            {results}
            </div>
        </div>
        );
    } else {
        return (
            <div>
        
        </div>
        )
    }
};

export default Rank;
4

0 回答 0