How to convert a javascript object to user presentation?

Asked

Viewed 106 times

1

I need to present the results of a firebase query to the user. To perform tests, debug code manually and check conditions, we usually use some function such as console.log, Alert, json returns, array, etc., during development. But at some point, the data of these queries will have to be presented on the screen to the user, and the last thing we want is for them to see some data structure tree with keys, brackets, etc.

In my specific case, I am using firebase, and using the documentation codes.

  <script type="text/javascript">
    var db = firebase.firestore();

    var docRef = db.collection("cities").doc("SF");

    docRef.get().then(function(doc) {
        if (doc.exists) {
            console.log("Document data:", doc.data());
        } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
        }
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });




    var citiesRef = db.collection("cities");

    var query = citiesRef.where("state", "==", "CA");

    </script>

The question is how I take data from these objects (docRef and query) and present it in a user-friendly way.

  • Maybe something like JSONT can help you.

1 answer

0


There are some libraries specialized in this, take a look at: https://json2html.com

There’s a new feature in Chrome that can also help, called console.table(), that displays a stylized table with information on the console

  • Fantastic this solution! obg! Leave here the code snippet for anyone who goes through the same problem: <script src="https://cdnjs.cloudflare.com/ajax/libs/json2html/1.2.0/json2html.min.js"></script> var citiesRef = db.Collection("Cities"); var query = citiesRef.Where("state", "==", "CA"); var t = {'<>':'div','html':'${country} ${year}'}; var d = [ { "capital": false, "country": "USA", "name": "San Francisco", "Population": 860000, "state": "CA" } ]; Document.write( json2html.Transform(d,t) );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.