简体   繁体   English

检查空对象并返回布尔值

[英]Check for empty object and return boolean

I have a AddContactForm form that allows the user to add contacts .我有一个AddContactForm表单,允许用户添加contacts

When the user fills in the conactNumber - onBlur it checks if this conactNumber already exists.当用户填写conactNumber - onBlur它会检查这个conactNumber已经存在。

How can I make The CheckIfContactExists function returns either true or false instead of the promise object?如何让CheckIfContactExists函数返回truefalse而不是 promise 对象?

Please note that I can't change the returned value from the api, it only return a contact object.请注意,我无法更改 api 的返回值,它只返回contact对象。

export default class AddContactForm extends Component {
  state = {
   ...
  };

  checkContact = () => {
    const { contactNumber } = this.state.newContactInfo;
    CheckIfContactExists(contactNumber); //return promise
  };

 render() {
   ...
    return (  
       ...
   );
  }
}

const CheckIfContactExists = async searchString => {
  const { data: contactsInfo } = await axios.get(`api/Contacts/SearchContact?contactNum=${searchString}`);
};

Make use of async await in checkContact just like you did for CheckIfContactExists .在 checkContact 中使用 async await 就像你在CheckIfContactExists所做的CheckIfContactExists Also return the boolean result from CheckIfContactExits method还从CheckIfContactExits方法返回布尔结果

export default class AddContactForm extends Component {
  state = {
   ...
  };

  checkContact = async () => {
    const { contactNumber } = this.state.newContactInfo;
    try {
      const res = await CheckIfContactExists(contactNumber); 
      return res;
    } catch (e) {
       console.log('Error', error);
    }
  };

 render() {
   ...
    return (  
       ...
   );
  }
}

const CheckIfContactExists = async searchString => {
  const { data: contactsInfo } = await axios.get(`api/Contacts/SearchContact?contactNum=${searchString}`);
  if (Object.keys(contactsInfo).length > 0) {
     return true;
  } else {
      return false;
  }
};

You can't make it return just a boolean since it's an asynchronous operation.你不能让它只返回一个布尔值,因为它是一个异步操作。 You could make the checkContact function async as well and await it.您也可以使checkContact函数asyncawait它。

Example例子

export default class AddContactForm extends Component {
  state = {
   // ...
  };

  checkContact = async () => {
    const { contactNumber } = this.state.newContactInfo;
    const contactInfo = await CheckIfContactExists(contactNumber);

    this.setState({
      contactNumberTaken: Object.keys(contactInfo).length !== 0
    });
  };

  render() {
    // ...
  }
}

暂无
暂无

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

相关问题 如何根据数组对象javascript中的条件检查返回布尔值 - how to return boolean value based on condition check in array object javascript 通过选择器检查对象是否存在,并使用jQuery或Javascript返回布尔值 - Check if an object exists by its selector, return a boolean value with jQuery or Javascript 我怎样才能返回一个空对象,同时在JavaScript中该空对象等于boolean false? - how can I return an empty object meanwhile the empty object equals to boolean false in javascript? 如何返回布尔值并检查该布尔值 - How to return boolean observable and check that boolean value 我想知道如何检查文件输入是否为空并返回布尔值 - I would like to know how to check if a file input is empty and return a boolean 循环遍历对象并检查字段是否为空数组并返回 null - Loop through object and check if field is an empty array and return null 如何使用 javascript 或 lodash 返回指示 object 数组元素为空的 boolean 值 - How to return a boolean value indicating an element of an array of object is empty using javascript or lodash 流星:检查对象或布尔值的参数 - Meteor: check parameter for object or boolean 检查 object 的所有值,然后如果值为空,它将返回该空值的键是什么? - Check all the values of an object then if the values is empty it will return what is the key of that empty value? 检查对象是否在Nunjucks中为空 - Check if an object is empty in Nunjucks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM