Ivshti / linvodb3

Persistent database for Node.js/NW.js/Electron with MongoDB/Mongoose-like features and interface on top of LevelUp
MIT License
747 stars 71 forks source link

Error while new LinvoDB() #56

Open alexluoli opened 8 years ago

alexluoli commented 8 years ago

I Wrote code in Electron Main-Process:

var LinvoDB = require("linvodb3");

// The following two lines are very important // Initialize the default store to level-js - which is a JS-only store which will work without recompiling in NW.js / Electron LinvoDB.defaults.store = { db: require("level-js") }; // Comment out to use LevelDB instead of level-js // Set dbPath - this should be done explicitly and will be the dir where each model's store is saved LinvoDB.dbPath = process.cwd();

var Doc = new LinvoDB("doc", { /* schema, can be empty */ })

Then, while run it, error thow:

App threw an error during load ReferenceError: self is not defined at new IDBStore (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/idb-wrapper/idbstore.js:129:56) at Level._open (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/level-js/index.js:37:14) at Level.AbstractLevelDOWN.open (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/abstract-leveldown/abstract-leveldown.js:28:17) at LevelUP.open (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/levelup.js:117:6) at new LevelUP (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/levelup.js:87:8) at LevelUP (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/levelup.js:47:12) at Function.Model.initStore (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/linvodb3/lib/model.js:79:100) at new Model (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/linvodb3/lib/model.js:52:35) at Object. (/Users/alexluoli/SVN/Medical/Electron/OT-Training/main.js:253:11) at Module._compile (module.js:556:32)

How to solve it? thank you!

Ivshti commented 8 years ago

Can you try level-js 2.x.x? Maybe it's some kind of incompatibility

alexluoli commented 8 years ago

$ npm list level-js ottraining@1.0.0 /Users/alexluoli/SVN/Medical/Electron/OT-Training ├── level-js@2.2.4 └─┬ linvodb3@3.25.0 └── level-js@2.2.4 extraneous

I am using 2.2.4, which version would be fit? thanks!

alexluoli commented 8 years ago

when comment this line:

// LinvoDB.defaults.store = { db: require("level-js") }; // Comment out to use LevelDB instead of level-js

other error throw:

App threw an error during load LevelUPError: Failed to require LevelDOWN (Module version mismatch. Expected 50, got 48.). Try npm install leveldown if it's missing at requireError (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/util.js:60:10) at getLevelDOWN (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/util.js:54:11) at LevelUP.open (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/levelup.js:114:37) at new LevelUP (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/levelup.js:87:8) at LevelUP (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/levelup/lib/levelup.js:47:12) at Function.Model.initStore (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/linvodb3/lib/model.js:79:100) at new Model (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/linvodb3/lib/model.js:52:35) at Object. (/Users/alexluoli/SVN/Medical/Electron/OT-Training/main.js:252:11) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10)

alexluoli commented 8 years ago

I found this: https://github.com/TomAshley303/electron-abi-fix

sh electron-abi-fix.sh 50 1.4.1

Done!

wgerven commented 8 years ago

I am running plain node and have exactly the same issue. code:

var LinvoDB = require("linvodb3");
var modelName = "doc";
var options = { };
options.filename = "./dbs/linvo.db";  
options.store = { db: require("level-js") };  
var collection = new LinvoDB(modelName, {}, options); // New model; Doc is the constructor

LinvoDB.dbPath // default path where data files are stored for each model
LinvoDB.defaults // default options for every model

collection.insert({hello:'world'}, function(){});

error:

/test/node_modules/level-js/node_modules/idb-wrapper/idbstore.js:129
        var env = typeof window == 'object' ? window : self;
                                                       ^

ReferenceError: self is not defined
    at new IDBStore (/test/node_modules/level-js/node_modules/idb-wrapper/idbstore.js:129:56)
    at Level._open (/test/node_modules/level-js/index.js:37:14)
    at Level.AbstractLevelDOWN.open (/test/node_modules/level-js/node_modules/abstract-leveldown/abstract-leveldown.js:28:17)
    at LevelUP.open (/test/node_modules/linvodb3/node_modules/levelup/lib/levelup.js:117:6)
    at new LevelUP (/test/node_modules/linvodb3/node_modules/levelup/lib/levelup.js:87:8)
    at LevelUP (/test/node_modules/linvodb3/node_modules/levelup/lib/levelup.js:47:12)
    at Function.Model.initStore (/test/node_modules/linvodb3/lib/model.js:79:100)
    at new Model (/test/node_modules/linvodb3/lib/model.js:52:35)
    at Object.<anonymous> (/test/linvodb_init.js:10:18)
    at Module._compile (module.js:409:26)

npm ERR! Linux 3.13.0-96-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "linvo-init"
npm ERR! node v4.6.0
npm ERR! npm  v2.15.9
npm ERR! code ELIFECYCLE

I am running linvodb3@3.25.0 and level-js@2.2.4. I tried some older versions of level-js, but that didn't solve the issue. Going through idbstore.js, indeed I cannot find a self being defined...

wgerven commented 8 years ago

Any idea how to resolve my (above) issue? I'd be interested in testing your db for a current electron project...

milu2003 commented 8 years ago

hello, you create db in Main-Process, and how you use it in Renderer-Process, can you tell me?thanks!

PMK89 commented 7 years ago

I got exactly the same problem and I already tried for three weeks to get LevelDB working in electron on windows so that's no option for me. Any ideas how to fix this error?

milu2003 commented 7 years ago

@PMK89 I also spent a lot of time here Finally, my solution is only in the main run. The request can be made through the socket interface

PMK89 commented 7 years ago

@milu2003 Thanks. I already only use it in the main process, but still get this error.

milu2003 commented 7 years ago

@PMK89
What is the problem? I now use very well In fact, I do not use linvodb3,Because my data may be large, hundreds of thousands of millions。 Linvodb3 Queries are very slow

I suggest you use https://www.npmjs.com/package/levelup directly

PMK89 commented 7 years ago

@milu2003 Thank you very much for your help. As I wrote I get exactly the same error as the OP (ReferenceError: self is not defined at new IDBStore. ...), It needs to work with electron on windows (problem with leveldown seems to be an electron issue: https://github.com/electron/electron/issues/5851) Also I already wrote a lot of code for LinvoDB and it works fine on my node/express dev-server. I unsuccessfully addressed the issue already on the electron forums: https://discuss.atom.io/t/error-cannot-find-module-linvodb3-tried-everything-nothing-works/35877 https://discuss.atom.io/t/third-party-libraries-for-electron-with-webpack-angular2/36189 and stackoverflow: http://stackoverflow.com/questions/40683090/electron-require-node-modules-in-main-process-error-cannot-find-module-linvod?noredirect=1#comment68615703_40683090

edit: I use the electron-quick-start at the moment to prevent any interference from other components.

PMK89 commented 7 years ago

@Ivshti

It seems to be a general issue with the level-js version. I just tried it on my dev server (that worked fine with the LevelDB version) and got a very similar error:

{ [Function: Model]
  defaults: { autoIndexing: true, autoLoad: true, store: { db: [Object] } },
  dbPath: './data/element_test14' }
D:\Informatik\CMtestserver\node_modules\idb-wrapper\idbstore.js:129
        var env = typeof window == 'object' ? window : self;
                                                       ^

ReferenceError: self is not defined
    at new IDBStore (D:\Informatik\CMtestserver\node_modules\idb-wrapper\idbstore.js:129:56)
    at Level._open (D:\Informatik\CMtestserver\node_modules\level-js\index.js:37:14)
    at Level.AbstractLevelDOWN.open (D:\Informatik\CMtestserver\node_modules\abstract-leveldown\abstract-leveldown.js:28:17)
    at LevelUP.open (D:\Informatik\CMtestserver\node_modules\levelup\lib\levelup.js:117:6)
    at new LevelUP (D:\Informatik\CMtestserver\node_modules\levelup\lib\levelup.js:87:8)
    at LevelUP (D:\Informatik\CMtestserver\node_modules\levelup\lib\levelup.js:47:12)
    at Function.Model.initStore (D:\Informatik\CMtestserver\node_modules\linvodb3\lib\model.js:79:100)
    at new Model (D:\Informatik\CMtestserver\node_modules\linvodb3\lib\model.js:52:35)
    at Object.<anonymous> (D:\Informatik\CMtestserver\controllers\elementController.js:13:17)
    at Module._compile (module.js:570:32)

OS: Windows_NT 10.0.14393 node v6.9.1 npm v3.10.8

milu2003 commented 7 years ago

@PMK89
I have not used level-js, can not help you.

If your data is small, I suggest you use nedb

https://github.com/louischatriot/nedb

PMK89 commented 7 years ago

@milu2003 But you got leveldown running in electron on windows? That was the cause of my original problem. I already tried NeDB, it works on electron, but I couldn't implement the query I need with it's API:

 $or: [
    {$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
    {$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
]

So I switched to LinvoDB3, it works fine on my node/express dev-server.

It's for a cognitive-mapping/mind-mapping app. And the querying is similar to a regular map, x0,y0 are start coordinates x1,y1 end coordinates, if one of them is in the current view it's loaded. Besides this there are few simple single queries by id, title, priority, date etc.

Cause only content (max 10kb, mean 500byte per entry) by a single user is stored or queried performance shouldn't be an issue. I already wrote the scripts for mongoDB/Mongoose so if you know any alternatives where I can use these with electron would also help me a lot.

milu2003 commented 7 years ago

@PMK89

leveldown can run on windows. I use it very well。 I redesigned the index and encapsulated api according to my own needs The current query is fast.

Because it is their own things, problems are relatively easy to solve. The workload is not as big as you think. You spent three weeks to solve the problem, not as a start to write

milu2003 commented 7 years ago

@PMK89

You have to compiler before use it, for example:

 "scripts": {
    "start": "electron main.js",
    "postinstall": "cd node_modules/leveldown && node-gyp rebuild --target=1.2.1 --arch=x64 --dist-url=https://atom.io/download/atom-shell"
  },

Start the program using the command

npm start
PMK89 commented 7 years ago

@milu2003 Thanks a lot, now it works. I thought electron-rebuilt would do exactly that cause it also uses node-gyp but somehow that wasn't the case.