加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – UnityWebRequest.downloadHandler返回null,同时从Node Ser

发布时间:2020-12-15 08:38:24 所属栏目:百科 来源:网络整理
导读:我正在Unity上创建一个注册场景.在后端我有MongoDB的NodeJS服务器.注册成功,数据也保存在Mongo上. 这是我的NodeJS api用于注册 api.post('/register',(req,res) = {Account.register(new Account({username: req.body.username}),req.body.password,function
我正在Unity上创建一个注册场景.在后端我有MongoDB的NodeJS服务器.注册成功,数据也保存在Mongo上.

这是我的NodeJS api用于注册

api.post('/register',(req,res) => {
Account.register(new Account({username: req.body.username}),req.body.password,function(err,account){
  console.log("acc: "+account);
  if(err){
    if (err.name == "UserExistsError") {
      console.log("User Exists");
      return res.status(409).send(err);
    }else {
      console.log("User Error 500");
      return res.status(500).send(err);
    }
  }else {
    let newUser = new User();
    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);
        return res.send(err);
      }else{
        console.log('user saved');
        res.json({ message: 'User saved' });
      }
    });
    passport.authenticate(
      'local',{
        session: false
      })(req,res,() => {
         res.json({ registermsg: 'Successfully created new account'});
      });
  }
});
});

这是我在Unity C中的POST协同程序#

IEnumerator Post(string b) {

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(b);

    using (UnityWebRequest www = new UnityWebRequest(BASE_URL,UnityWebRequest.kHttpVerbPOST)) {
        UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
        www.uploadHandler = uH;
        www.SetRequestHeader("Content-Type","application/json");
        yield return www.Send();

        if (www.isError) {
            Debug.Log(www.error);
        } else {
            lbltext.text = "User Registered";
            Debug.Log(www.ToString());
            Debug.Log(www.downloadHandler.text);
        }
    }
}

我正在尝试Debug.Log(www.downloadHandler.text);但是我得到了NullReferenceException.

我想问一下,我正在使用的方式在我的api中返回响应是否正确?如果是,我如何在Unity端使用该响应.

解决方法

UnityWebRequest.Post,UnityWebRequest.Get和其他创建UnityWebRequest新实例的UnityWebRequest functions将自动附加DownloadHandlerBuffer.

现在,如果使用UnityWebRequest构造函数创建UnityWebRequest的新实例,则DownloadHandler不会附加到该新实例.您必须使用DownloadHandlerBuffer手动执行此操作.

IEnumerator Post(string b)
{

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(b);

    using (UnityWebRequest www = new UnityWebRequest(BASE_URL,UnityWebRequest.kHttpVerbPOST))
    {
        UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
        DownloadHandlerBuffer dH = new DownloadHandlerBuffer();

        www.uploadHandler = uH;
        www.downloadHandler = dH;
        www.SetRequestHeader("Content-Type","application/json");
        yield return www.Send();

        if (www.isError)
        {
            Debug.Log(www.error);
        }
        else
        {
            lbltext.text = "User Registered";
            Debug.Log(www.ToString());
            Debug.Log(www.downloadHandler.text);
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读