2017-04-08 234 views
5

我正在Unity上創建註冊場景。在後端,我有NodeJS服務器和MongoDB。註冊成功並且數據也保存在Mongo上。UnityWebRequest.downloadHandler返回null,同時從節點服務器獲得響應

這是我的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'}); 
     }); 
    } 
}); 
}); 

這是我POST協同例程在Unity C#

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

我想問一下,我用我的方式返回響應正確嗎?如果是的話,我該如何在Unity方面使用該響應。

回答

5

UnityWebRequest.PostUnityWebRequest.GetUnityWebRequestfunctions創建的UnityWebRequest新實例,會自動將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); 
     } 
    } 
} 
+0

謝謝,我對使用'DownloadHandlerBuffer'感到困惑。但是現在我得到了'設置後無法發送標題'。我讀了很多問題,但無法讓我的代碼按照它們工作。 –

+0

我打算將標題重命名爲「UnityWebRequest.downloadHandler返回null」。這將有助於很多人遇到這個問題。爲您的新標題問題創建一個新問題將會很好。如果你描述那個問題是什麼,那更好。 – Programmer

+0

好的,我會改變這個問題。 –

相關問題