You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在C# Razor Pages中无法从Firebase数据库获取用户全部字段

问题:登录后仅能获取用户姓名,其余字段为null(Firebase数据库已填充完整数据)

用户登录后,仪表板仅能展示名字和姓氏,调试发现Email、ProfilePicURL等字段均为null,但Firebase数据库中所有字段都已填充完整数据。

错误截图显示:Email、ProfilePicURL字段值为null,FirstName和LastName有正常数据。

相关代码

public static User User 
{ 
    get 
    { 
        return GetUser().Result;   
    } 
    set 
    { 
        if(value != _user)
        {
            _user = value;
        }
    } 
}

public async static void Init(IJSRuntime js)
{
#if Debug
HostURL = "http://localhost:5001/"
#endif
    jsRuntime = js;
    Module = await js.InvokeAsync<IJSObjectReference>("import", "/js/bundle.js");
}

public async static Task<User> GetUser()
{
    try
    {
        if (_user == null)
        {
            var response = await jsRuntime.InvokeAsync<string>("VerifyData");
            var jsonResponse = JsonConvert.DeserializeObject<Response>(response);
            var newuser = new User();
            newuser.UID = jsonResponse.uid;
            newuser.Token = jsonResponse.token;
            response = await API.VerifyGetUser(jsonResponse.uid, jsonResponse.token);
            jsonResponse = JsonConvert.DeserializeObject<Response>(response);
            newuser.FirstName = jsonResponse.firstName;
            newuser.LastName = jsonResponse.lastName;
            newuser.ProfilePicURL = jsonResponse.profilePicURL;
            newuser.Email = jsonResponse.email;
           
            _user = newuser;
        }
        return _user;

    }
    catch (Exception ex)
    {
        Main.Log.Log($"GetUser: {ex}", "ERROR");
    }

    jsRuntime.InvokeAsync<string>("RemoveVerifyData");
    return null;
}

可能的原因及解决步骤

1. Response类字段映射不匹配

检查Response类的属性名称、大小写是否和API返回的JSON字段完全一致。比如API返回的JSON字段是email(小写),但Response类里定义的是Email(大写),就会导致反序列化失败,字段为null。

解决:使用JsonProperty特性指定JSON字段名,确保映射正确:

public class Response
{
    public string uid { get; set; }
    public string token { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    [JsonProperty("profilePicURL")] // 匹配JSON中的字段名
    public string profilePicURL { get; set; }
    [JsonProperty("email")] // 匹配JSON中的字段名
    public string email { get; set; }
}

2. API接口返回数据不完整

API.VerifyGetUser方法返回的JSON可能未包含email、profilePicURL等字段,导致反序列化后这些字段为null。

解决:在代码中添加日志输出API返回的原始响应内容,确认数据是否完整:

response = await API.VerifyGetUser(jsonResponse.uid, jsonResponse.token);
Main.Log.Log($"VerifyGetUser 响应内容: {response}", "INFO"); // 新增日志
jsonResponse = JsonConvert.DeserializeObject<Response>(response);

根据日志内容确认API是否返回了所需字段,若未返回,需要检查后端接口逻辑,确保从Firebase读取完整数据。

3. 异步调用死锁

User属性的getter中直接调用GetUser().Result,这会阻塞主线程,可能导致异步方法GetUser未完全执行就返回,部分字段未赋值。

解决:避免在属性getter中同步等待异步方法,改为提供异步方法获取用户:

// 替换原有的User属性
public static async Task<User> GetCurrentUser()
{
    return await GetUser();
}

在需要获取用户数据的地方使用await GetCurrentUser(),确保异步流程正确执行。

4. 异常未被正确排查

GetUser方法中的catch块会捕获异常并返回null,但可能异常导致部分字段未赋值,同时后续执行RemoveVerifyData清除验证数据,影响后续重试。

解决:查看Main.Log中记录的错误信息,根据异常内容定位问题(比如API调用失败、JSON反序列化错误等),针对性修复。


内容的提问来源于stack exchange,提问作者Faris Irfan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:36:17