Giter Site home page Giter Site logo

Comments (9)

akhoury avatar akhoury commented on June 28, 2024 1

ok, so here's what's happening, you're trying to setItem() before the storage has finished initializing, and creating the the storage directory, that's why it only does it the first time, the second time the directory is already created.

When you use storage.init(), it's asynchronous, you need to wait for it to be ready before you can use the storage

This is my fault, the example in the README was wrong, I just fixed it

you can solve this in 2 ways, either,

Wait for it to be done, before using it

storage.init().then(()=> {
    // start your app here.
});

OR

Use the synchronous version of init - this is OK if you are doing it once at app-load time, it will not really impact performance

storage.initSync();
// start your app here.

from node-persist.

akhoury avatar akhoury commented on June 28, 2024

are you deleting the storage folder while your application is running?

or are you killing the app, deleting the storage folder, then restarting to see the ENOENT error?

from node-persist.

akhoury avatar akhoury commented on June 28, 2024

Also, can you provide a full stack trace please, I just need lines numbers, feel free to obfuscate the full filepaths

from node-persist.

Jan-Ka avatar Jan-Ka commented on June 28, 2024

Thanks for the fast response!

I'm terminating node, removing the storage files and folder and then starting the application again.

Here's what I got:

// console output
d:\path\to\project\projectname\node_modules\node-persist\src\local-storage.js:36
        if (err) throw err;
                 ^
Error: ENOENT: no such file or directory, open 'd:\path\to\project\projectname\server\storage\6a99c575ab87f8c7d1ed1e52e7e349ce'
    at Error (native)

//npm-debug.log
14 verbose stack Error: [email protected] dev: `npm run startInternal`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:242:16)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at EventEmitter.emit (events.js:191:7)
14 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\spawn.js:40:14)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at ChildProcess.emit (events.js:191:7)
14 verbose stack     at maybeClose (internal/child_process.js:852:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)
15 verbose pkgid [email protected]

I hope that's everything? Can't seem to convince node to provide any more information.

Tried debugging but can't see anything out of the ordinary

//callstack from debugging, backtracing from thrown error
noop                    local-storage.js    36
(anonymous function)    local-storage.js    447
(anonymous function)    fs.js               1305
(anonymous function)    fs.js               123

I can see the folder was created successfully on the FS.

from node-persist.

akhoury avatar akhoury commented on June 28, 2024

can you provide the following:
• node --version
• the version of the node-persist package you are using
• show me the block of the code that you are using to init/initSync the storage instance.

Also, are you using some sort a master Node process to spawn and handle other Node processes? like a cluster or something? can you share what npm run startInternal is doing? I suspect you have a multiple processes and one of them is not getting killed.

This is really weird, the test cases always delete the storage dirs with no issues, maybe there is a case we didn't cover.

I'd be happy to help but I need a little more. Is your project open source?

from node-persist.

Jan-Ka avatar Jan-Ka commented on June 28, 2024

No, it's not open source - but I will try to provide all information:

d:\path\to\project\projectname>node --version
v6.4.0

d:\path\to\project\projectname>npm -v node-persist
3.10.3

And the npm scripts

"scripts": {
    "start": "npm run build && npm run startInternal",
    "build": "webpack -p --config webpack.production.config.js",
    "startInternal": "node -r dotenv-safe/config server"
  }

//during development, app is started with 'npm run startInternal'

As well as the storage init:

// server\index.js - entry point
const storage = require("node-persist");

storage.init({
    dir: "server/storage"
});

Nothing is interacting with storage in any way.

I try to recreate the problem in a different project that I can share.

from node-persist.

Jan-Ka avatar Jan-Ka commented on June 28, 2024

Here you go: https://github.com/Jan-Ka/verbose-succotash

from node-persist.

akhoury avatar akhoury commented on June 28, 2024

awesome! thanks for this! i will take a look tonight

from node-persist.

Jan-Ka avatar Jan-Ka commented on June 28, 2024

Thank you for the quick fix - i just tested it and it works fine.

Somehow I didn't think of testing the Problem without setting a variable :)

Code for reference:

const storage = require("node-persist");

let storageInit = storage.init({
    dir: "server/storage"
});

const argvs = process.argv.slice(2);

if (argvs[0] === "setup") {
    console.log("INIT", "SETITEM", "WAIT 5s", "EXIT");

// here be changes
    storageInit.then(() => {
        storage.setItem("demo",
            [
                {
                    "demo": "data"
                }
            ]
        ).then(
            function () {
                console.log("SETITEM SUCCESSFULL");
            },
            function (reason) {
                console.log("SETITEM ERROR", reason);
            });
    });
} else {
    console.log("INIT", "WAIT 5s", "EXIT");
}

setTimeout(() => {
    process.exit();
}, 5000);

from node-persist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.