检索 Firebase 创建的 ID Post
Retrieve ID created by Firebase Post
我正在为 Unity 使用 Firebase API,我正在使用 Post 请求将一些数据推送到实时数据库
在使用该 Post 请求后,我需要为新节点创建的 ID 来执行其他操作,但我不确定如何检索该 ID。
Firebase.Instance.Post(uri, new Dictionary<string, string>() { { "name", name } },
delegate { Debug.Log("Name has been added successfully!"); //here is where i need the new ID to do something else },
delegate { Debug.Log("Something Wrong! .. Please try again later"); });
post函数:
public void Post<T, K>(URI uri, T body, System.Action<K> onSuccess, System.Action<string> onFail)
{
RequestHelper currentRequest = new RequestHelper
{
Uri = uri.Path,
BodyString = JsonConvert.SerializeObject(body),
IgnoreHttpException = true
};
Debug.Log("BODY_post: " + currentRequest.BodyString);
RestClient.Post(currentRequest, (exception, res) => ResolveResponse(exception, res, onSuccess, onFail));
}
void ResolveResponse<T>(RequestException exception, ResponseHelper res, System.Action<T> onSuccess, System.Action<string> onFail)
{
string returnedText = res.Text;
AuthError authError = null;
try
{
authError = JsonConvert.DeserializeObject<AuthError>(returnedText);
}
catch (System.Exception ex)
{
Debug.Log(ex);
}
finally
{
if (authError != null && authError.error != null && authError.error.message != null)
{
onFail(BeautifyMessage(authError.error.message));
}
else if (exception != null && (exception.IsHttpError || exception.IsNetworkError))
{
onFail(BeautifyMessage(exception.Message));
}
else if (typeof(T) == typeof(string))
{
onSuccess((T)(object)returnedText);
}
else
{
onSuccess(JsonConvert.DeserializeObject<T>(returnedText));
}
}
}
并调用其余 api 的库 post 函数。
您正在包装 Firebase REST API,其中 calling POST
return 是响应中的键:
A successful request is indicated by a 200 OK HTTP status code. The response contains the child name of the new data specified in the POST request.
{ "name": "-INOQPH-aV_psbk3ZXEX" }
因此您的响应处理程序将需要解析该结果,并且 return 将 "-IN...."
键返回给调用者。
我正在为 Unity 使用 Firebase API,我正在使用 Post 请求将一些数据推送到实时数据库 在使用该 Post 请求后,我需要为新节点创建的 ID 来执行其他操作,但我不确定如何检索该 ID。
Firebase.Instance.Post(uri, new Dictionary<string, string>() { { "name", name } },
delegate { Debug.Log("Name has been added successfully!"); //here is where i need the new ID to do something else },
delegate { Debug.Log("Something Wrong! .. Please try again later"); });
post函数:
public void Post<T, K>(URI uri, T body, System.Action<K> onSuccess, System.Action<string> onFail)
{
RequestHelper currentRequest = new RequestHelper
{
Uri = uri.Path,
BodyString = JsonConvert.SerializeObject(body),
IgnoreHttpException = true
};
Debug.Log("BODY_post: " + currentRequest.BodyString);
RestClient.Post(currentRequest, (exception, res) => ResolveResponse(exception, res, onSuccess, onFail));
}
void ResolveResponse<T>(RequestException exception, ResponseHelper res, System.Action<T> onSuccess, System.Action<string> onFail)
{
string returnedText = res.Text;
AuthError authError = null;
try
{
authError = JsonConvert.DeserializeObject<AuthError>(returnedText);
}
catch (System.Exception ex)
{
Debug.Log(ex);
}
finally
{
if (authError != null && authError.error != null && authError.error.message != null)
{
onFail(BeautifyMessage(authError.error.message));
}
else if (exception != null && (exception.IsHttpError || exception.IsNetworkError))
{
onFail(BeautifyMessage(exception.Message));
}
else if (typeof(T) == typeof(string))
{
onSuccess((T)(object)returnedText);
}
else
{
onSuccess(JsonConvert.DeserializeObject<T>(returnedText));
}
}
}
并调用其余 api 的库 post 函数。
您正在包装 Firebase REST API,其中 calling POST
return 是响应中的键:
A successful request is indicated by a 200 OK HTTP status code. The response contains the child name of the new data specified in the POST request.
{ "name": "-INOQPH-aV_psbk3ZXEX" }
因此您的响应处理程序将需要解析该结果,并且 return 将 "-IN...."
键返回给调用者。