错误-Parse error. '}' expected
我在使用此代码时遇到问题,在该代码中,我发送调用一些数据以将它们放置在图形中,我占据了拒绝并使用react进行工作,但找不到解决方法。
vehiculoPorColores = () => {
const _this = this
fetch("/live/graph/color")
.then(response => response.json())
.then(datos => {
const vehicleKey = Object.keys(datos);
console.log(datos)
_this.setState(
{
vehiculosC: vehicleKey.map((e, i) => (
{
name: vehicleKey[i], datos ,[e]
}
)
},() => {setTimeout(this.vehiculoPorColores, 1000)}
)
})
}
答案 0 :(得分:0)
您缺少结束符}
,错误消息表示它期望结束符。当定义多个对象值时,通常的做法是将这些值放在单独的行上。
此{ name: vehicleKey[i], datos, [e] }
变为:
{
name: vehicleKey[i],
datos,
[e]
}
在对象键分配中帮助发现错误,例如语法错误[e]
。
这是产生语法错误的原因:
const baz = 'baz'
const syntaxErrorExample = {
foo: 'tar',
[baz] // <-- Syntax error
}
未捕获的SyntaxError:意外令牌}
可以更改为:
const baz = 'baz'
const example = {
foo: 'tar',
[baz]: undefined // <-- Works
}
class Foo {
vehiculoPorColores = () => {
fetch("/live/graph/color")
.then(response => response.json())
.then(datos => {
const vehicleKey = Object.keys(datos);
const newState = {
vehiculosC: vehicleKey.map((e, i) => {
return {
name: vehicleKey[i],
datos,
[e]: undefined
}
})
}
this.setState(newState, () => {
setTimeout(this.vehiculoPorColores, 1000)
})
})
}
}
console.log(true) // Made it to this line, so no syntax error
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>