67:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "… is not valid JSON P

huangapple go评论82阅读模式
英文:

67:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON P

问题

你遇到的问题似乎是JSON解析错误,可能是因为代码中的HTML实体编码没有正确处理。你可以尝试将代码中的实体编码转换回正常字符。以下是修复后的代码:

import React from 'react'
import { Link } from 'react-router-dom';

export default function Vans() {
    const [vans, setVans] = React.useState([]);
    React.useEffect(() => {
        fetch("/api/vans")
            .then(res => res.json())
            .then(data => setVans(data.vans))
    }, []);

    const vanElements = vans.map(van => (
        <div key={van.id} className="van-tile">
            <Link to={`/vans/${van.id}`}>
                <img src={van.imageUrl} />
                <div className="van-info">
                    <h3>{van.name}</h3>
                    <p>${van.price}<span>/day</span></p>
                </div>
                <i className={`van-type ${van.type} selected`}>{van.type}</i>
            </Link>
        </div>
    ));

    return (
        <div className="van-list-container">
            <h1>Explore our van options</h1>
            <div className="van-list">
                {vanElements}
            </div>
        </div>
    )
}

此外,请确保你的服务器端代码也没有HTML实体编码问题。希望这可以帮助你解决问题。

英文:

I'm doing a tutorial from scrimba for react and react router 6 and I'm encountering an error with the provided data. The error says 67:1 Uncaught (in promise) SyntaxError: Unexpected token &#39;&lt;&#39;, &quot;&lt;!DOCTYPE &quot;... is not valid JSON
P
and is at line 9 which is .then(data =&gt; setVans(data.vans)). The whole code for Vans.jsx is:

import React from &#39;react&#39;
import { Link } from &#39;react-router-dom&#39;

export default function Vans() {
    const [vans, setVans] = React.useState([])
    React.useEffect(() =&gt; {
        fetch(&quot;/api/vans&quot;)
            .then(res =&gt; res.json())
            .then(data =&gt; setVans(data.vans))
    }, [])

    const vanElements = vans.map(van =&gt; (
        &lt;div key={van.id} className=&quot;van-tile&quot;&gt;
            &lt;Link to={`/vans/${van.id}`}&gt;
                &lt;img src={van.imageUrl} /&gt;
                &lt;div className=&quot;van-info&quot;&gt;
                    &lt;h3&gt;{van.name}&lt;/h3&gt;
                    &lt;p&gt;${van.price}&lt;span&gt;/day&lt;/span&gt;&lt;/p&gt;
                &lt;/div&gt;
                &lt;i className={`van-type ${van.type} selected`}&gt;{van.type}&lt;/i&gt;
            &lt;/Link&gt;
        &lt;/div&gt;
    ))

    return (
        &lt;div className=&quot;van-list-container&quot;&gt;
            &lt;h1&gt;Explore our van options&lt;/h1&gt;
            &lt;div className=&quot;van-list&quot;&gt;
                {vanElements}
            &lt;/div&gt;
        &lt;/div&gt;
    )
}

The data from the api is coming from server.js which is:

import { createServer, Model } from &quot;miragejs&quot;


createServer({
    models: {
        vans: Model,
    },

    seeds(server) {
        server.create(&quot;van&quot;, { id: &quot;1&quot;, name: &quot;Modest Explorer&quot;, price: 60, description: &quot;The Modest Explorer is a van designed to get you out of the house and into nature. This beauty is equipped with solar panels, a composting toilet, a water tank and kitchenette. The idea is that you can pack up your home and escape for a weekend or even longer!&quot;, imageUrl: &quot;https://assets.scrimba.com/advanced-react/react-router/modest-explorer.png&quot;, type: &quot;simple&quot; })
        server.create(&quot;van&quot;, { id: &quot;2&quot;, name: &quot;Beach Bum&quot;, price: 80, description: &quot;Beach Bum is a van inspired by surfers and travelers. It was created to be a portable home away from home, but with some cool features in it you won&#39;t find in an ordinary camper.&quot;, imageUrl: &quot;https://assets.scrimba.com/advanced-react/react-router/beach-bum.png&quot;, type: &quot;rugged&quot; })
        server.create(&quot;van&quot;, { id: &quot;3&quot;, name: &quot;Reliable Red&quot;, price: 100, description: &quot;Reliable Red is a van that was made for travelling. The inside is comfortable and cozy, with plenty of space to stretch out in. There&#39;s a small kitchen, so you can cook if you need to. You&#39;ll feel like home as soon as you step out of it.&quot;, imageUrl: &quot;https://assets.scrimba.com/advanced-react/react-router/reliable-red.png&quot;, type: &quot;luxury&quot; })
        server.create(&quot;van&quot;, { id: &quot;4&quot;, name: &quot;Dreamfinder&quot;, price: 65, description: &quot;Dreamfinder is the perfect van to travel in and experience. With a ceiling height of 2.1m, you can stand up in this van and there is great head room. The floor is a beautiful glass-reinforced plastic (GRP) which is easy to clean and very hard wearing. A large rear window and large side windows make it really light inside and keep it well ventilated.&quot;, imageUrl: &quot;https://assets.scrimba.com/advanced-react/react-router/dreamfinder.png&quot;, type: &quot;simple&quot; })
        server.create(&quot;van&quot;, { id: &quot;5&quot;, name: &quot;The Cruiser&quot;, price: 120, description: &quot;The Cruiser is a van for those who love to travel in comfort and luxury. With its many windows, spacious interior and ample storage space, the Cruiser offers a beautiful view wherever you go.&quot;, imageUrl: &quot;https://assets.scrimba.com/advanced-react/react-router/the-cruiser.png&quot;, type: &quot;luxury&quot; })
        server.create(&quot;van&quot;, { id: &quot;6&quot;, name: &quot;Green Wonder&quot;, price: 70, description: &quot;With this van, you can take your travel life to the next level. The Green Wonder is a sustainable vehicle that&#39;s perfect for people who are looking for a stylish, eco-friendly mode of transport that can go anywhere.&quot;, imageUrl: &quot;https://assets.scrimba.com/advanced-react/react-router/green-wonder.png&quot;, type: &quot;rugged&quot; })
    },

    routes() {
        this.namespace = &quot;api&quot;

        this.get(&quot;/vans&quot;, (schema, request) =&gt; {
            return schema.vans.all()
        })
        
        this.get(&quot;/vans/:id&quot;, (schema, request) =&gt; {
            const id = request.params.id
            return schema.vans.find(id)
        })
    }
})

I've copied and pasted the code from the tutorial but is still getting the error. Anyone know what I'm missing here?

答案1

得分: 2

你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容。以下是要翻译的内容:

"You're running this on your local right? I had the same issue. The problem was an import in the index.jsx file.

On Scrimba, they have import &quot;./server&quot;
I had to simply add a dot import &quot;../server&quot; because the local react build put the index.jsx file in the src folder."

英文:

You're running this on your local right? I had the same issue. The problem was an import in the index.jsx file.

On Scrimba, they have import &quot;./server&quot;
I had to simply add a dot import &quot;../server&quot; because the local react build put the index.jsx file in the src folder.

huangapple
  • 本文由 发表于 2023年5月14日 18:41:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定