简体   繁体   English

反应:解析错误:意外的令牌,预期的“,”

[英]React: Parsing error: Unexpected token, expected “,”

I am merely trying to check a condition (invitesForEvent.length) and run a map loop on invitesForEvent, but am getting the error shown below:我只是想检查一个条件(invitesForEvent.length)并在invitesForEvent上运行一个map循环,但我得到了如下所示的错误:

I am merely trying to check a condition (invitesForEvent.length) and run a map loop on invitesForEvent, but am getting the error shown below:我只是想检查一个条件(invitesForEvent.length)并在invitesForEvent上运行一个map循环,但我得到了如下所示的错误:

./src/Invites.js
  Line 36:  Parsing error: Unexpected token, expected ","

  34 |     console.log("this.state.invitesForEventData.length: " + 
this.state.invitesForEventData.length );
  35 |     return (
> 36 |         {invitesForEvent.length <= 0
     |                         ^
  37 |             ? 'NO ENTRIES YET'
  38 |             : invitesForEvent.map((dat) => (
  39 |                 <div>Event invite entry: {invitesForEvent.length}</div>

import React from 'react';
import axios from 'axios';
import CSS from './css/styles.css';

class Invites extends React.Component {

  state = {
    invitesForEventData: [{test: []}]
  }

  componentDidMount() {
    this.getInvitesForEvent(this.props.eventid);
  }

  getInvitesForEvent = (eventid) => {

    console.log("eventid: " + eventid)
    axios.post('http://localhost:3001/api/getInvitesForEvent', {
        eventid: eventid
    })
    .then((response) => {
        console.log("response.data.data: ", response);
        this.setState({ invitesForEventData: response.data.data })
    })
    .catch(function (error) {
      console.log(error);
    });

  };

  render() {
    const invitesForEvent = this.state.invitesForEventData;

    console.log("this.state.invitesForEventData.length: " + 
this.state.invitesForEventData.length );
    return (
        {invitesForEvent.length <= 0
            ? 'NO ENTRIES YET'
            : invitesForEvent.map((dat) => (
                <div>Event invite entry: {invitesForEvent.length}</div>
            ))
        }
    )
  }
}

export default Invites;

You are switching the logic at that time.您当时正在切换逻辑。 Use a fragment or directly return:使用片段或直接返回:

return (<>
{invitesForEvent.length <= 0
  ? 'NO ENTRIES YET'
  : invitesForE

Or use this way:或者这样使用:

return invitesForEvent.length <= 0
        ? 'NO ENTRIES YET'
        : invitesForEvent.map((dat) => (
            <div>Event invite entry: {invitesForEvent.length}</div>
        ))

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

相关问题 解析错误:意外的令牌,应为“;” 在反应 JS - Parsing error: Unexpected token, expected “;” in react JS React - 解析错误:意外的令牌,预期的“;” - React - Parsing error: Unexpected token, expected “;” 反应:解析错误:意外的令牌,预期为“…” - React: Parsing error: Unexpected token, expected “…” 解析错误:意外的令牌,预期的“,” JavaScript React - Parsing error: Unexpected token, expected “,” JavaScript React 解析错误:意外的标记,预期的“,” - Parsing error: Unexpected token, expected "," 解析错误:意外的标记,应为“...” - Parsing error: Unexpected token, expected "..." 反应引导。 特别是解析错误:意外的标记,预期的“,” - React-Bootstrap. Specifically with Parsing error: Unexpected token, expected "," React组件构造函数(解析错误:意外令牌,期望“}”) - React component constructor (Parsing error: Unexpected token , expected “}”) React-Redux - 解析错误:意外的令牌,预期的“,”在行动创建者 - React-Redux - Parsing error: Unexpected token, expected “,” in action creator React 组件'解析错误:意外的令牌,预期的“;”'渲染后 - React Component 'Parsing error: Unexpected token, expected “;”' after render
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM