2017-04-08 9 views
5

Unityで登録シーンを作成しています。バックエンドでは、私はMongoDBとNodeJSサーバーを持っています。登録は成功し、データもMongoに保存されます。NodeWebRequest.downloadHandlerがヌルを返し、ノードサーバからの応答を取得中

これはレジスタ

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'}); 
     }); 
    } 
}); 
}); 

のための私のNodeJSのAPIであり、これはユニティ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を取得し、私のPOSTコルーチンです。

私は質問したいと思いますが、私のAPIで応答を返すために使用している方法は正しいですか?はいの場合、どのようにUnity側でその応答を使用できますか?

答えて

5

UnityWebRequest.PostUnityWebRequest.GetUnityWebRequestの新しいインスタンスを作成し、他のUnityWebRequestfunctionsは、自動的にDownloadHandlerBufferは、それに接続しているだろう。今

、あなたはDownloadHandlerその新しいインスタンスに接続ないUnityWebRequestコンストラクタ、とUnityWebRequestの新しいインスタンスを作成する場合。 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

さて、私は質問を変更します。 –

関連する問題