Home > Blockchain >  Reading Firestore document with a scanner
Reading Firestore document with a scanner

Time:09-29

I have a barcode scanner and Firestore database. All the Firestore document ids are barcodes. I can manually fetch a doc by typing an id into the app but I want to get them back by scanning from a device. This line I've blocked out in the code below (with the number 9310080105764) will get that doc back and if I scan any barcode, will get that same doc back.

    //private final DocumentReference noteRef = db.collection("Ratings").document("9310080105764");

I want the scan to get the corresponding doc to their id numbers. I need it to understand the scanned result is the doc id. Any hints?

 //Initialise
private TextView textViewData;
private CodeScanner mCodeScanner;
private Button btClear;
private Result result;
private Result documentId;

private final FirebaseFirestore db = FirebaseFirestore.getInstance();

//The below line will call that doc // private final DocumentReference noteRef = db.collection("Ratings").document("9310080105764");

//This line won't get a doc, I think this is where I'm going wrong?
private final DocumentReference noteRef = db.collection("Ratings").document(String.valueOf(documentId));

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Assign
    btClear = findViewById(R.id.bt_clear);
    textViewData = findViewById(R.id.text_view_data);
    CodeScannerView scannerView = findViewById(R.id.scanner_view);
    mCodeScanner = new CodeScanner(this, scannerView);

    //documentId = result;
        
    mCodeScanner.setDecodeCallback(new DecodeCallback() {
        public void onDecoded(final Result result) {

            noteRef.get()
                    .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void onSuccess(DocumentSnapshot documentSnapshot) {
                            if (documentSnapshot.exists()) {
                                String Brand = documentSnapshot.getString(KEY_BRAND);
                                String Product = documentSnapshot.getString(KEY_PRODUCT);
                                String Owned = documentSnapshot.getString(KEY_OWNED);
                                String Made = documentSnapshot.getString(KEY_MADE);
                                String Ingredients = documentSnapshot.getString(KEY_INGREDIENTS);
                                String Info = documentSnapshot.getString(KEY_INFO);

CodePudding user response:

When you are using the following reference:

DocumentReference noteRef = db.collection("Ratings").document("9310080105764");

You will indeed always get the same document since you are using a fixed document ID. To solve this, what you need to do, is to get the result from the scan operation and use the corresponding number like this:

mCodeScanner.setDecodeCallback(new DecodeCallback() {
    public void onDecoded(final Result result) {
        DocumentReference ratingsRef = db.collection("Ratings")
        String number = result.getText();
        ratingsRef.document(number).get().addOnSuccessListener(/* ... /*);
        ...
    }
}
  • Related