当前位置: 首页 > 编程语言 > C#

UnityWebRequest.downloadHandler在从NodeServerShare获取响应时返回null

时间:2023-04-10 15:20:45 C#

UnityWebRequest.downloadHandler在从NodeServer获取响应时返回null我正在Unity上创建一个注册场景。在后端,我有一个带有MongoDB的NodeJS服务器。注册成功,数据也保存在Mongo上。这是我的NodeJSapi使用于注册api.post('/register',(req,res)=>{Account.register(newAccount({username:req.body.username}),req.body.password,function(err,account){console.log("acc:"+account);if(err){if(err.name=="UserExistsError"){console.log("UserExists");returnres.status(409).send(err);}else{console.log("UserError500");returnres.status(500).send(err);}}else{letnewUser=newUser();newUser.accountid=account._id;newUser.name=req.body.fullname;newUser.gender=req.body.gender;newUser.role=req.body.role;newUser.country=req.body.country;newUser.coins=req.body.coins;newUser.save(err=>{if(err){console.log(err);returnres.send(err);}else{console.log('usersaved');res.json({message:'Usersaved'});}});passport.authenticate('local',{session:false})(req,res,()=>{res.json({registermsg:'成功创建新的帐户'});});}});});这是我在UnityC中的POST协程#IEnumeratorPost(stringb){byte[]bytes=System.Text.Encoding.ASCII.GetBytes(b);使用(UnityWebRequestwww=newUnityWebRequest(BASE_URL,UnityWebRequest.kHttpVerbPOST)){UploadHandlerRawuH=newUploadHandlerRaw(bytes);www.uploadHandler=uH;www.SetRequestHeader("Content-Type","application/json");收益回报www.Send();如果(www.isError){Debug.Log(www.error);}else{lbltext.text="用户注册";Debug.Log(www.ToString());调试日志(www.downloadHandler.text);我正在尝试Debug.Log(www.downloadHandler.text);但是我收到了NullReferenceException我想问一下顺便问一下,我用来在我的api中返回响应的方式是否正确?如果是,我如何在Unity端使用该响应。UnityWebRequest.Post、UnityWebRequest.Get和其他创建UnityWebRequest新实例的UnityWebRequest函数将自动附加DownloadHandlerBuffer。现在,如果使用UnityWebRequest构造函数创建UnityWebRequest的新实例,则DownloadHandler不会附加到该新实例。您必须使用DownloadHandlerBuffer手动执行此操作。IEnumeratorPost(stringb){byte[]bytes=System.Text.Encoding.ASCII.GetBytes(b);使用(UnityWebRequestwww=newUnityWebRequest(BASE_URL,UnityWebRequest.kHttpVerbPOST)){UploadHandlerRawuH=newUploadHandlerRaw(bytes);DownloadHandlerBufferdH=newDownloadHandlerBuffer();www.uploadHandler=uH;www.downloadHandler=dH;www.SetRequestHeader("Content-Type","application/json");收益回报www.Send();如果(www.isError){Debug.Log(www.error);}else{lbltext.text="用户注册";Debug.Log(www.ToString());调试日志(www.downloadHandler.text);你很简单你不需要附加一个DownloadHandlerBuffer,你不需要一个新的UnityWebRequest!DownloadHandlerBufferdH=newDownloadHandlerBuffer();www.downloadHandler=dH;以上是C#学习教程:UnityWebRequest.downloadHandler返回null,同时获取NodeServer响应的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注—本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: