javascriptreactjsdatabasenedb

nedb Database not loading file in React Js


Anybody who's knowledgeable using NeDB?

import React from "react";
const Datastore = require("nedb"),
  database = new Datastore({ filename: "./database.db", autoload: true });

database.loadDatabase();

const App = () => {
  return <div>Hello</div>;
};

export default App;

I just want to load an empty database file.. I tried multiple ways of picking the file path but it doesn't seem to appear.


Solution

  • Creating a new database file won't work in the browser, you'll have to run that on the server to create a file:

    On the server:

    const Datastore = require("nedb"),
      database = new Datastore({ filename: "./database.db", autoload: true });
    

    On the client:

    import React from "react";
    
    const App = () => {
      return <div>Hello</div>;
    };
    
    export default App;
    

    On the client, you can use nedb to create an in-memory DataStore, but it won't be persisted to a file.