node-json-db icon indicating copy to clipboard operation
node-json-db copied to clipboard

Unable to use JsonDB while exported

Open rockysoft8 opened this issue 4 years ago • 0 comments

Hello,

I am trying to use the JsonDB in a electron package compiled via rollup but it seems I am unable to export the JsonDB object / methods. Can someone please take a look and let me know what the issue could be?

db.js

'use strict'
import { JsonDB } from 'node-json-db';
import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
const log = require('../logger/logger');

let db;
if (process.env.NODE_ENV == 'production') {
    try {
        db = new JsonDB(new Config(process.env.DBPATH_PROD, true, true, '/'));
    } catch (error) {
        log.error("Database file not found: " + error)
    }
}
else {
    try {
        db = new JsonDB(new Config(process.env.DBPATH_DEV, true, true, '/'));
    } catch (error) {
        log.error("Database file not found: " + error)
    }
}
exports.db = db;

controller.js

let jsonDB = require('./database/db.js')
var testData = jsonDB.db.getData('/data/test')
console.log(testData)

Error

`App threw an error during load
TypeError: Cannot read properties of undefined (reading 'getData')
`

rockysoft8 avatar Jan 28 '22 20:01 rockysoft8