简体   繁体   English

我如何在 react.js 中搜索失败时返回错误消息

[英]How would I return an error message upon a failed search in react.js

I'm currently learning how to code the front end of a webpage in react.js and in order to complete the assignment, I need to return a message saying Campground not found when a user enters anything into the search bar that's not in my database .我目前正在学习如何在react.jsreact.js网页的前端代码,为了完成作业,当用户在搜索栏中输入任何不在我的database内容时,我需要返回一条消息说找不到 Campground .

For example:例如:

If they type Banana instead of a real campground name , it needs to display the message.如果他们输入Banana而不是真正的campground name ,则需要显示消息。 I'm sure it's relatively simple I'm just at a complete loss at this point.我确信这相对简单,我现在完全不知所措。

Below is the code for my find.js file:下面是我的find.js文件的代码:

import Layout from '../components/MyLayout.js';
import React from "react";
import {getInfo} from '../lib/utils.js';

class Find extends React.Component {
  constructor(props) {
    super(props);
    this.state={search: ""};
  }

  handleUpdate(evt){
    this.setState({search: evt.target.value});
  }

  async handleSearch(evt){
    const park = await getInfo(this.state.search);
    this.setState({park});
  }

render() {
  return (
    <Layout>
    <div style={{ margin: "auto auto", width: "600px", textAlign: "center" }}>
        <h1>New Mexico Camground Search</h1>
        <p><input type='text' value={this.state.search} onChange={this.handleUpdate.bind(this)}/></p>
        <div className="button-style" onClick={this.handleSearch.bind(this)}>Submit</div>
        {this.state.park ? <div>
        <br />
        <h3>{this.state.park.name}</h3> 
            <br /><img style={{maxWidth: '400px', maxHeight: "400px"}}
            src={this.state.park.image_url} /> <br />
            <h3>{this.state.park.closest_town}</h3>
            <p>{this.state.park.description} </p>
          </div> : null}
      <style jsx>{`
        h1,
        a {
          font-family: 'Sans';
          color: #ffa500;
        }
        h2,
        a {
            font-family: 'Sans'
            color: #ffa500;
        }
        .button-style {
            margin: auto auto;
            cursor: pointer;
            background-color: #ffa500;
            color: #ffffff;
            width: 100px;
            font-family: "Sans";
        }
        ul {
          padding: 10;
        }
        li {
          list-style: none;
          margin: 5px 0;
        }
        a {
          text-decoration: none;
          color: blue;
        }
        a:hover {
          opacity: 0.6;
        }
      `}</style>
      </div>
    </Layout>
      )
  }
    }
}

export default Find;

You can do it with try/catch你可以用try/catch来做

async handleSearch(evt) {
  try { 
    const park = await getInfo(evt.target.value);
    this.setState({park});
  } catch(error) {
    // do something with error
  }
}

(You can also use the target value directly) (也可以直接使用目标值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 新手尝试 react.js 项目。 当我尝试导入图像时,我收到一条无法编译的错误消息 - Newbie trying a react.js project. When I try to import an image I get an error message that it failed to compile 如何在React.JS中添加搜索过滤器? - How Can I Add Search Filter in React.JS? 如何使用promise返回更新react.js组件? - How do I update a react.js component with a promise return ? React.js:如何将搜索查询从 react.js 前端发送到 express.js 后端? - React.js: How do I send search query from react.js frontend to express.js backend? 如何搜索对象 react.js javascript? - How to search objects react.js javascript? React.js:如何创建搜索过滤器? - React.js: how to create search filter? 我如何在react.js中像卡片一样显示上传的照片? - How would I display uploaded photos like a stack of cards in react.js? 我将如何使用通过script标签嵌入有react.js的视频播放器 - How would I use a video player thats embedded through a script tag with react.js 在React.js中,我如何设置一个简单的全局事件系统来在组件之间进行通信? - In React.js, how would I set up a simple global event system to communicate between components? 在React.js中,如何从组件的字符串表示中呈现组件? - In React.js, how would I render a component from a string representation of a component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM