简体   繁体   中英

"not a function" when function being used as function parameter

I am not knowledgeable in NodeJS or JS so bare that in mind. I have been playing around with a radix trie module Merkle-Patricia-Tree and it's taken me down a rabbit hole when using a level module similar to Google's leveldb to store the radix trie in persistent storage.

what I think I know :

  1. The Merkle-Patricia-Tree module requires a levelup dependency which wraps level down as the first parameter when instantiating a new trie
  2. Seems to write to the store with trie.put(key,value) , but having problems reading with trie.get(key)
  3. In one of the error files in Markle-Patricia-Trie library...
constructor(leveldb) {
        this._leveldb = leveldb !== null && leveldb !== void 0 ? leveldb : level();
    }

does pass in the levelup(leveldown('path')) when instantiating trie with db but somehow says that here in the same file (included affected files)...

async get(key) {
        let value = null;
        try {
            value = await this._leveldb.get(key, exports.ENCODING_OPTS);
        }
        catch (error) {
            if (error.notFound) {
                // not found, returning null
            }
            else {
                throw error;
            }
        }
        return value;
    }

this...

(node:17128) UnhandledPromiseRejectionWarning: TypeError: this._leveldb.get is not a function
    at CheckpointDB.get (\cryptoNetwork\node_modules\merkle-patricia-tree\dist\db.js:27:41)
    at CheckpointDB.get (\cryptoNetwork\node_modules\merkle-patricia-tree\dist\checkpointDb.js:85:35)
    at SecureTrie.lookupNode (\cryptoNetwork\node_modules\merkle-patricia-tree\dist\baseTrie.js:250:31)
    at SecureTrie._lookupNode (\cryptoNetwork\node_modules\merkle-patricia-tree\dist\baseTrie.js:266:21)
    at \cryptoNetwork\node_modules\merkle-patricia-tree\dist\util\walkController.js:41:40
    at new Promise (<anonymous>)
    at WalkController.startWalk (\cryptoNetwork\node_modules\merkle-patricia-tree\dist\util\walkController.js:36:22)
    at Function.newWalk (\cryptoNetwork\node_modules\merkle-patricia-tree\dist\util\walkController.js:32:24)
    at SecureTrie.walkTrie (\cryptoNetwork\node_modules\merkle-patricia-tree\dist\baseTrie.js:221:47)
    at \cryptoNetwork\node_modules\merkle-patricia-tree\dist\baseTrie.js:200:28
(node:17128) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, 
or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:17128) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  1. The encoding is dealt with properly so assume under that axiom

aggregator code :

testing()
    {
        programData.volatile.modularVariables.action = 'read';
        programData.volatile.modularVariables.user = 'blahblahblahblah';
        programData.volatile.modularVariables.createTrie();
    };

object with relevant code (I'm aware of extra unneeded code, editing time of writing this)

//assume object containing these functions/variables inside

user: '-', //aggregator initializes this and action
action: '-',
createTrie: ()=>{switch(programData.volatile.modularVariables.action){case 'read':programData.volatile.modularVariables.read(programData.persistent.paths.srcRoot,programData.volatile.modularVariables.encoding.utf8,programData.volatile.modularVariables.handleTrie);break;case 'write':programData.volatile.modularVariables.read(programData.persistent.paths.srcRoot,programData.volatile.modularVariables.encoding.utf8,programData.volatile.modularVariables.handleTrie);break;case 'new':programData.volatile.modularVariables.newTrie(db);break;default: console.log(`%c${programData.volatile.debug.debugStrings.errCodes.createTrie}: Error with action selection`,'color: red');}},
handleTrie: async (err,data)=>{var userBIN = Buffer.from(programData.volatile.modularVariables.user); var root=Buffer.from(data,programData.volatile.modularVariables.encoding.hex);console.log(root); switch(programData.volatile.modularVariables.action){case 'read': const trie = new merkle_patricia_tree_1.SecureTrie(programData.persistent.paths.srcRoot,root); 
            var result = await trie.get(userBIN); console.log(result)}},
read: (file,encoding,cb)=>{fs.readFile(file,encoding,cb)},
encoding: {utf8:'utf8',hex:'hex',base64:'base64',BIN:(data,encoding)=>{Buffer.from(data,encoding)}}, //Depricate BIN asap,
srcRoot: './vortex/root.txt',


conclusion I am stumped, I am not sure what I am doing wrong or what is wrong with Markle-Patricia-Trie (or the leveldb). My questions are, what am I doing wrong? Can you explain to me what's wrong?. Thank you :).

在用作读取箭头函数回调的 handleTrie 箭头函数中,我传入了商店的路径,而不是关卡实例本身...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM