簡體   English   中英

向 Firebase 插入數據並在 xamarin 表單中對 UI(用戶名和密碼)設置限制-UPDATE

[英]Insert data to Firebase and set restriction to UI(username and password) in xamarin form-UPDATE

我的第一篇文章在這里。 我已經在這里解釋了我的問題,我的 output 是預期的。 Guangyu Bai 爵士 - MSFT 評論了我的帖子,您查看我的帖子並滾動他的答案。 我將他的回答應用到我的代碼中。 鏈接是https://stackoverflow.com/questions/74587990/insert-data-to-firebase-and-set-restriction-to-uiusername-and-password-in-xama/74596335?noredirect=1#comment131744947_74596335

這就是應用他的代碼的方式。 用戶界面中的我的用戶名字段

`

<Entry 
                                Placeholder="Username"
                                TextColor="Black"
                                HorizontalOptions="FillAndExpand"
                                Margin="0,0,15,0"
                                PlaceholderColor="Black"
                                x:Name="entryField_Username"
                                TextChanged="entryField_Username_TextChanged"/>

` textchanged 后面的代碼..

`

 async private void entryField_Username_TextChanged(object sender, TextChangedEventArgs e)
        {
            var oldText = e.OldTextValue;
            var newText = e.NewTextValue;
          //  var newText = entryField_Username.Text;
            CUSTOMER customer = new CUSTOMER();
            // var person = await firebaseHelper.GetPerson(Convert.ToString(EntryText.Text);

            var customerName = await customerRepo.GetCustomer(Convert.ToString(entryField_Username.Text));
            //if (oldText != string.Empty)
            //{
            //    if(await customerRepo.GetCustomer(Convert.ToString(entryField_Username.Text))
            //        {

            //    }
            //}

            if (customerName != null)
            {

                entryField_Username.Text = customer.CusFirstName;
                // this is a Toast method
                // await this.DependencyService.Get<Toast>().Show("Username Already Exist!");
                await this.DisplayToastAsync("Username already exist.", 1500);
            }
            else
            {

            }
        }

`

在我的 customerRepository 文件中,如果代碼或 function 是 ..

`

 public async Task<List<CUSTOMER>> GetAllCustomer()
        {

            return (await firebaseClient
              .Child("CUSTOMER")
              .OnceAsync<CUSTOMER>()).Select(item => new CUSTOMER
              {
                  CusFirstName = item.Object.CusFirstName,
                  CusID = item.Object.CusID
              }).ToList();
        }

        public async Task<CUSTOMER> GetCustomer(string customerName)
        {

            // var data = await firebaseClient.Child(nameof(Customer)).PostAsync(JsonConvert.SerializeObject(customer));


            //if (!string.IsNullOrEmpty(data.Key))
            //{
            //    return true;
            //}
            //return false;


            var allCustomer = await GetAllCustomer();
            await firebaseClient
              .Child("CUSTOMER")
              .OnceAsync<CUSTOMER>();
            return allCustomer.Where(a => a.CusFirstName == customerName).FirstOrDefault();

            //  var allCustomer = await firebaseClient.Child(nameof("Customer")).OnceAsync<Customer>
        }

`

同樣在那個 customerRepository 文件中,我用我的實時數據庫的鏈接設置了我的 firebaseclient。

現在,我的流程是,當我輸入用戶的用戶名並點擊鍵盤上的“檢查”時,就像我附上的圖像一樣。 圖片來自谷歌,我只是對其進行了編輯,但這就是我在創建時所做的視覺效果 它會崩潰並且會出現錯誤。 那是錯誤:(

請幫助我掌握如何解決錯誤並實現我的問題。 任何鏈接都會對我的帖子發表評論,將不勝感激,謝謝。

編輯; 基於用戶對我的帖子的評論,所以我檢查我的實時數據庫,這是我們的規則`


{
  "rules": {
    "some_path": {
      "$uid": {
        // Allow only authenticated content owners access to their data
        ".read": "auth !== null && auth.uid === $uid",
        ".write": "auth !== null && auth.uid === $uid"
      }
    }
  }
}


`

如果我們這樣設置規則,我們會得到這個錯誤..

Firebase.Database.FirebaseException: '處理請求時發生異常。 Url: https://************************************************ *******************/.json?print=silent Request Data: {/*我把這個注釋掉了,但這部分就像我的表屬性和它的數據*/ } 響應:{“錯誤”:“權限被拒絕”}'

這個問題是由服務器端拒絕的數據引起的。

由於此異常是因為 Firebase 台服務器拒絕了該操作而引發的,因此解決此問題的方法是更改 Cloud Firestore Security Rules中的規則。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM