Skip to content

persisted Json objects always access filePath of most recently created object #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tebemis opened this issue Aug 7, 2019 · 0 comments

Comments

@tebemis
Copy link
Member

tebemis commented Aug 7, 2019

Rporter: @tmoher

here’s the behavior:

source code:

var alpha = nutella.persist.getJsonObjectStore('alpha');
console.log(alpha.filePath());

var beta = nutella.persist.getJsonObjectStore('beta');
console.log(alpha.filePath());
console.log(beta.filePath());

var gamma = nutella.persist.getJsonObjectStore('gamma');
console.log(alpha.filePath());
console.log(beta.filePath());
console.log(gamma.filePath());

console output:

data/TEST-TEST-0/alpha.json
data/TEST-TEST-0/beta.json
data/TEST-TEST-0/beta.json
data/TEST-TEST-0/gamma.json
data/TEST-TEST-0/gamma.json
data/TEST-TEST-0/gamma.json

• same problem with load. it will always load from the most recent pathName.

• same problem in getJsonObjectCollection

my amateur analysis:

somehow alpha and beta are losing their pathNames.
i’m a js newbie w.r.t. OOP, but it seems like the filePath
is shared among all instances of persisted objects.

if i make this change in json_file_persisted_object.js:

    Object.prototype.save = function(path) {
        fs.writeFileSync(path, JSON.stringify(this));
    };

and add the path explicitly in the save call, e.g.:

alpha.save(‘data/…/alpha.json’)

then everything works.

so, i have a workaround for now.

if you can’t fine a quick answer, could you replace
the save function in the nutella lib with:

    Object.prototype.save = function(path) {
        if (arguments.length == 1) fs.writeFileSync(path, JSON.stringify(this));
        else fs.writeFileSync(this.filePath(), JSON.stringify(this));
        
    };

so that my code will be compatible with any fixes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant