Store data using couchbase using nativescript kind of confusing for a newbie


#1

Hello guys, I’m having some problems with using couchbase lite when coding with nativescript/angular. The following scenario is my code, and I will like to explain it to you according to the way I have understood how to use couchbase lite to store data. Now if I have miss something, please let me know in how to achieve what I’m looking to process with my code, it will be very much appreciated.

import { Component, OnInit, ChangeDetectorRef, ViewContainerRef } from '@angular/core';
import { DrawerPage } from '../shared/drawer/drawer.page';
import { TextField } from 'ui/text-field';
import { Switch } from 'ui/switch';
import { Validators, FormBuilder, FormGroup } from '@angular/forms';
import { ModalDialogService, ModalDialogOptions } from "nativescript-angular/modal-dialog";
import { ReservationModalComponent } from "../reservationmodal/reservationmodal.component";
import { CouchbaseService } from '../services/couchbase.service';
import { Page } from 'ui/page';
import { Animation, AnimationDefinition } from 'ui/animation';
import { SwipeGestureEventData, SwipeDirection } from 'ui/gestures';
import { Color } from 'color';
import * as enums from 'ui/enums';
import { View } from 'ui/core/view';
import { Couchbase } from 'nativescript-couchbase';

@Component({
    selector: 'app-reservation',
    moduleId: module.id,
    templateUrl: './reservation.component.html'
})

export class ReservationComponent extends DrawerPage implements OnInit {

    reservations: FormGroup;
    docId: string = "reservations";
    showReservation: boolean = false;
    cardImage: View;


    constructor(private changeDetectorRef: ChangeDetectorRef,
        private formBuilder: FormBuilder,
        private modalService: ModalDialogService,
        private couchbaseservice: CouchbaseService,
        private page: Page,
        private vcRef: ViewContainerRef) {
            super(changeDetectorRef);

            this.reservations = this.formBuilder.group({
                guests: 3,
                smoking: false,
                dateTime: ['', Validators.required]
            });

            let doc = this.couchbaseservice.getDocument(this.docId);
            if(doc == null) {
                this.couchbaseservice.createDocument({"reservations": []}, this.docId);
            }
            else if (doc) {
                this.couchbaseservice.updateDocument(this.docId, {"reservations": this.reservations.value});
            }
            else {
                this.reservations = doc.reservations
            }

        }
    
    ngOnInit() {

    }

    onSmokingChecked(args) {
        let smokingSwitch = <Switch>args.object;
        if (smokingSwitch.checked) {
            this.reservations.patchValue({ smoking: true });
           
        }
        else {
            this.reservations.patchValue({ smoking: false });
        }
    }

    onGuestChange(args) {
        let textField = <TextField>args.object;
        this.reservations.patchValue({ guests: textField.text });
        
        
    }

    onDateTimeChange(args) {
        let textField = <TextField>args.object;
        this.reservations.patchValue({ dateTime: textField.text });
        
        
    }

    createModalView(args) {
        let options: ModalDialogOptions = {
            viewContainerRef: this.vcRef,
            context: args,
            fullscreen: false
        };
        this.modalService.showModal(ReservationModalComponent, options)
            .then((result: any) => {
                if (args === "guest") {
                    this.reservations.patchValue({ guests: result });
                }

                else if (args === "date-time") {
                    this.reservations.patchValue({ dateTime: result });

                }
            });
    }

    onSubmit() {
        console.log(JSON.stringify(this.reservations.value));
    }

}

Now the above code is to save a document that has a form to log a reservation. According to what was explain to me that to do the following process, one need to create a variable, in this instace I created a variable docId, which is the one that will create the document “reservations” id. Now, if you notice under the constructor, there is the form that was created to make the reservation.

Now this form is the document that I need to get using the getDocument method, but when I do that it doesn’t work.

Taking a look at some tutoriols videos, they states that with the getDocument method, I sould use it to get the document docId, and when such document is null, I should proceed to use the createDocument method to creat a new document to store with couchbase, now my question, is this, why should I need to creat a new document, when I already have the document? And when I use the updateDocument method, it doesn’t work, it gives me error. I hope I’m being clear on what I’m trying to achive, and hopefully one of you guys can assist me on this. Thanks in advance.