Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解码错误:失败“预期对象具有名为'gamegrid'的字段”_Javascript_Json_Elm_Decoder - Fatal编程技术网

Javascript 解码错误:失败“预期对象具有名为'gamegrid'的字段”

Javascript 解码错误:失败“预期对象具有名为'gamegrid'的字段”,javascript,json,elm,decoder,Javascript,Json,Elm,Decoder,我的代码ELM/JSON/Python Flask有问题,所以我的问题是 收到消息后,我的服务器尝试通过JSON向ELM视图发送响应。 我的浏览器控制台得到了这个 0: currentPlayerId:0 firstClickCellId:-1 游戏状态:1 游戏网格:Array9 0:{hash:0,值:-1} 1:{hash:1,值:-1} 2:{hash:2,值:-1} 3:{hash:3,值:-1} 4:{hash:4,值:-1} 5:{hash:5,值:-1} 6:{hash:6,值:

我的代码ELM/JSON/Python Flask有问题,所以我的问题是

收到消息后,我的服务器尝试通过JSON向ELM视图发送响应。 我的浏览器控制台得到了这个

0: currentPlayerId:0 firstClickCellId:-1 游戏状态:1 游戏网格:Array9 0:{hash:0,值:-1} 1:{hash:1,值:-1} 2:{hash:2,值:-1} 3:{hash:3,值:-1} 4:{hash:4,值:-1} 5:{hash:5,值:-1} 6:{hash:6,值:-1} 7:{hash:7,值:-1} 8:{hash:8,值:-1} 长度:9 __原型:阵列0 userToPlayId:1 __原型:对象 长度:1 __原型:阵列0 但是,我会在以下几点之后收到此消息:

解码错误:预期对象包含名为“gamegrid”的字段时失败 但是,gamegrid字段出现在收到的消息中!我不明白为什么它不能被解码

我把代码放进去,这样你就可以看到以前做了什么

========================代码============================================================

这是我模型的一部分

类型别名模型= { 用户:列表用户 ,gameOverView:gameOverView } 类型别名GameOverView= { gameGrid:列表单元格 ,userToPlayId:String ,gameOverStatus:String ,firstClickCellId:String ,currentPlayerId:String } 类型别名单元格= { 哈希:字符串 ,值:String } 这是这部分的解码器

小区解码器:解码器小区 手机解码器= Decode.map2单元 Decode.field哈希Decode.string Decode.field值Decode.string listCellDecoder:解码器列表单元格 ListCell解码器= 解码列表单元解码器 游戏概述解码器:解码器游戏概述 游戏概述解码器= Decode.map5游戏概述 Decode.field游戏网格listCellDecoder Decode.field userToPlayId Decode.string Decode.field gameOverStatus Decode.string Decode.field firstClickCellId Decode.string Decode.field currentPlayerId Decode.string 这是JSON解码器

decodeExternalGameOverView:值->消息 解码外部游戏概述= case Decode.decodeValue游戏概述 好的游戏概述-> GotNewGameOverView游戏概述 Err Err-> 解码错误 订阅:模型->子消息 订阅模式= 子批次 [userlistPort decodeExternalUserlist, 游戏概览列表端口解码外部游戏概览, receiveData ReceivedDataFromJS] 这是发送消息的服务器注意到他成功地收到了以前的消息

@app.route'/updategrid/',methods=['POST'] @flask_login.login_必需 def校验: 印刷品+++++++++++++++++++++++ firsCellClikId=request.json[firstClickedId] secondCellClikId=request.json[secondClickedId] userToPlayId=request.json[userToPlay] currentPlayerId=request.json[currentPlayerId] gameOverStatus=1 g=[] 对于范围9中的i: 网格={} 网格[hash]=stri grid[value]=request.json[stri] g、 附加网格 打印检查播放回合 printfirstCell=,firsCellClikId,SecondCell=,secondCellClikId printUserToplay=,userToPlayId,UserPlayerId=,currentPlayerId ifuserToPlayId==0: userToPlayId=1 其他: userToPlayId=0 对于g项目中的x、y: 发送={ 游戏网格:g, userToPlayId:userToPlayId, gameOverStatus:gameOverStatus, firstClickCellId:-1, currentPlayerId:currentPlayerId } 打印发送 io.emit'gamegrid'[ 邮寄 ] ,广播=True 打印已发送的新网格 返回ok,201 HTML模板的一部分

var-socket=io; var app=Elm.Main.init{node:document.querySelector'Main-content'}; socket.on'gamegrid',functiongamegrid{ console.logNew GameOverView已收到:,gamegrid; app.ports.gameOverViewlistPort.sendgamegrid; }; 小结:问题仍然存在:javascript控制台或者更确切地说,elm解码器在gamegrid字段存在时找不到它,并且与预期的类型单元格列表相对应

欢迎你的建议


致以最诚挚的问候::

我认为错误在这里:

io.emit('gamegrid', [
  
    send,
  
    ]
  , broadcast=True)
因为您将send dictionary/对象作为数组的唯一元素发送。您可以在控制台输出中看到这一点,并且似乎导致了错误,因为数组没有gamegrid属性-您想要的属性位于包含的对象上


将[send]替换为simply send,这样应该可以工作。

我认为错误在这里:

io.emit('gamegrid', [
  
    send,
  
    ]
  , broadcast=True)
因为您将send dictionary/对象作为数组的唯一元素发送。您可以在控制台输出中看到这一点,并且似乎导致了错误,因为数组没有gamegrid属性-您想要的属性位于包含的对象上


将[send]替换为simply send,这应该可以工作。

您要传递给解码器的json输入到底是什么?加上
o该问题更容易发现可能的错误。另外,如果您认为问题出在Elm解码器上,那么所有服务器代码都是无关的,只会使文章变得杂乱无章?在问题中加入这一点可以更容易地发现可能的错误。另外,如果你认为问题出在你的Elm解码器上,那么所有的服务器代码都是无关的,只是把文章弄得乱七八糟。哦,谢谢你,这就是问题所在,我以为向JSON发送数据总是在方括号[]内,我用圆括号代替了它,它成功了。谢谢。哦,谢谢,这就是问题所在,我认为向JSON发送数据总是在方括号内[],我用括号代替了它,它起作用了。非常感谢。