简体   繁体   English

如何捕获Firebase搜索错误?

[英]how do I catch firebase search error?

I have a function that takes user's input and search through firebase database. 我有一个功能,可以接受用户的输入并通过firebase数据库进行搜索。 What I want to do is catch the any error most especially when their is no match for the user input I want to display as something like "No match was found for the number entered". 我想做的是捕获任何错误,尤其是当它们与我要显示为“找不到所输入的号码”的用户输入不匹配时。

this is the function I'm using 这是我正在使用的功能

function searchNSN(data){

    var container = document.getElementById('searchresult'); 
    container.innerHTML = '';
    var FindNSN = document.getElementById('searchinput').value;

    firebase.auth().onAuthStateChanged((user) => {
        if (user) {
            var BusinessesId = firebase.auth().currentUser.uid;
            return database.ref('/Businesses/' + BusinessesId + '/Inventory/' + FindNSN).once('value').then(function(snapshot) {
                var Results = snapshot.val();
                var Productkey = Object.keys(snapshot.val())[0];
                var ResultCard = `
                  <h1 class="searchtitle first">${Results.ProductName}</h1>
                  <p class="searchtext">${Results.ProductDescription}<span class="class">${Results.NSN}</span></p>
                  <p class="showproductdetail" onclick="SHOWSEARCHDETAIL();"><a href> Show Product Details </a href></p>

                    `
                    container.innerHTML += ResultCard;

            });
        }
    })
}

I know the console logs the error but how do I implement catching the error and reporting it to the user? 我知道控制台会记录错误,但是如何实现捕获错误并将其报告给用户?

If the database location/reference does not contain any data, it will still trigger a value event but with an empty DataSnapshot (ie snapshot.val() is null ). 如果数据库位置/引用不包含任何数据,它仍将触发value事件,但DataSnapshot为空(即snapshot.val()null )。 You may check if snapshot.val() === null , and if it does, let the user know that there are no results. 您可以检查snapshot.val() === null ,如果是,请让用户知道没有结果。

Reference , the value event section. 参考值事件部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM