Couchbase how to update document


#1

I am using nativescript-couchbase plugin to save some data.
I’ve created a class in order to have a layer of abstraction and use something like myClass.property = value etc.
The class implements the singleton pattern and is implented as follows:

class UserSettings {
    static NOTSET_VALUE = "ns"; /*This value is returned if trying to get a not-yet existing string */
    private USER_SETTINGS_TABLE_NAME = 'usersettings';
    private USER_SETTINGS_OBJ_NAME = 'usersettings';
    private _database;
    private _userSettingsDocument: Models.UserSettings;
    private _userSettingsObj: Models.UserSettings;
    private static _instance :UserSettings;
    private constructor() {
        this._database = new CouchBaseModule.Couchbase(this.USER_SETTINGS_TABLE_NAME);
        this._userSettingsDocument = this._database.getDocument(this.USER_SETTINGS_OBJ_NAME);
        if (!this._userSettingsDocument) {
            this._userSettingsObj = {
                authToken: UserSettings.NOTSET_VALUE,
                uuid: UserSettings.NOTSET_VALUE,
                email: UserSettings.NOTSET_VALUE,
                username: UserSettings.NOTSET_VALUE,
                password: UserSettings.NOTSET_VALUE,
                firstTime: false,
                hashtagOfTheDay: UserSettings.NOTSET_VALUE,
                today: UserSettings.NOTSET_VALUE
            }
            this._database.createDocument(this._userSettingsObj, this.USER_SETTINGS_OBJ_NAME);
            this._userSettingsDocument = this._database.getDocument(this.USER_SETTINGS_OBJ_NAME);
        }
        else {
            this._userSettingsObj = this._userSettingsDocument;
        }
    }
    public static getInstance() :UserSettings{
        if(this._instance){
            return this._instance;
        }else{
            return new UserSettings();
        }
    }
    /*====== USERNAME GETTER AND SETTER ======*/
    get username(): string {
        let username = this._userSettingsDocument.username;
        return username;
    }
    set username(value: string) {
        this._userSettingsObj = this._database.getDocument(this.USER_SETTINGS_OBJ_NAME);
        this._userSettingsObj.username = value;
        this.updateDb();
    }
    private updateDb() {
        this._database.updateDocument(this.USER_SETTINGS_OBJ_NAME, this._userSettingsObj);
    }
}

The problem is that the updateDb() method throws the following error :frowning:
JS: DOCUMENT ERROR com.couchbase.lite.CouchbaseLiteException, Status: 409 (HTTP 409 conflict)

I have seen this issue, but I don’t understand the solution.
What am I doing wrong?


#2

If you print out both variables before you try to update, are they what you expect?


#3

Which both variables?


#4

The variables in the above, before you try to update.


#5

Yes they do. The first is the name of the document i’m trying to update and the second is the object itself.
Here is the output of the object:

JS: called set -> UUID = 9FuMnjXcoSPE0hzzdINHd23yyrQ2
JS: ======WHERE======
JS: UserSettings
JS: =======LOGGING======
JS: ======MESSAGE======
JS: === dump(): dumping members ===
JS: {
JS:     "_rev": "1-c4dd194ce76b35c2c67b665726ef4005",
JS:     "password": "ns",
JS:     "hashtagOfTheDay": "ns",
JS:     "username": "ns",
JS:     "uuid": "9FuMnjXcoSPE0hzzdINHd23yyrQ2",
JS:     "email": "ns",
JS:     "firstTime": false,
JS:     "authToken": "ns",
JS:     "_id": "usersettings",
JS:     "today": "ns"
JS: }
JS: === dump(): dumping function and properties names ===
JS: === dump(): finished ===
JS: ======WHERE======
JS: About to update document
JS: DOCUMENT ERROR com.couchbase.lite.CouchbaseLiteException, Status: 409 (HTTP 409 conflict)

#6

Hey @ullal.aaron,

I created a project just to see if there was a problem with the plugin versus a problem somewhere in your code. My project is Angular, but bare with me:

import { Component } from "@angular/core";
import { Couchbase } from "nativescript-couchbase";

@Component({
    selector: "ns-app",
    templateUrl: "app.component.html",
})
export class AppComponent {

    private database: any;

    public constructor() {
        this.database = new Couchbase("test");
        let id = this.database.createDocument({ "firstname": "Nic", "lastname": "Raboy" }, "user");
        console.log("ID: " + id);
        console.dump(this.database.getDocument("user"));
        this.database.updateDocument("user", { "firstname": "Nicolas", "lastname": "Raboy" });
        console.dump(this.database.getDocument("user"));
    }

}

After running the project, here is what I get in the logs:

Successfully started on device with identifier 'emulator-5554'.
JS: ID: user
JS: === dump(): dumping members ===
JS: {
JS:     "lastname": "Raboy",
JS:     "_rev": "1-b0bab4b8a1bfeaba17b8c20bf9dea6fc",
JS:     "firstname": "Nic",
JS:     "_id": "user"
JS: }
JS: === dump(): dumping function and properties names ===
JS: === dump(): finished ===
JS: === dump(): dumping members ===
JS: {
JS:     "lastname": "Raboy",
JS:     "firstname": "Nicolas",
JS:     "_rev": "2-c9808600905a6a6df255d4f43aba05b9",
JS:     "_id": "user"
JS: }
JS: === dump(): dumping function and properties names ===
JS: === dump(): finished ===
JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.

I can tell the update worked because the revision number increased. Is there a possibility that your singleton isn’t truly behaving like a singleton? When you’re testing, are you always starting with a fresh document? I ask because maybe you’re having some issue with your revision numbers, which may be related to the singleton thing?

If your project isn’t sensitive, I’d be happy to test it on my end if you put it on GitHub. Other than that, I can’t seem to be able to reproduce. I’m doubtful it is a Core vs Angular thing.

Best,


#7

Hey you’re right!! I forgot a line in the constructor…the singleton was not implemented properly.
Thank you so much for your help, and sorry for the waste of time!


#8

Glad you got it! Never a waste of time :slight_smile:

In case you’d like to write a tutorial about whatever you’re doing (sounds interesting), check this out:

https://blog.couchbase.com/community-writing-program/

Make sure you use my name if it interests you.


#9

Fantastic!
I’ve emailed Laura and mentioned you.
Thank you again :slight_smile: