question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Key encoding bad behaviour

See original GitHub issue

The main function of the library, which is exported when you use require('levelup') have different behaviour if you use the option keyEncoding instead of valueEncoding, at least in the Json case.

I have forked the repository to help but I dont know if the problem it’s in the documentation or in the core code, so I just warn.

This is a really simple code to repeat the bug:


var levelup = require('levelup')

var db_a = levelup('./database_a',{ keyEncoding:'json' }),
    db_b = levelup('./database_b',{ valueEncoding:'json' }),
    t    = {'a':1, 'b':{ 'a':'text', 'b':'more_text' }, 'c':3 };

db_a.put('name', t, function (err) {
  if (err) return console.log('Ooops!', err);

  db_a.get('name', function (err, value) {
    if (err) return console.log('Ooops!', err);

    console.log('db_a:');
    console.log('typeof value = ',typeof value);
    console.log(value);
  })
});

db_b.put('name', t, function (err) {
  if (err) return console.log('Ooops!', err);

  db_b.get('name', function (err, value) {
    if (err) return console.log('Ooops!', err);

    console.log('db_b:');
    console.log('typeof value = ',typeof value);    
    console.log(value);
  })
});

And this is the output:

db_b:
typeof value =  object
{ a: 1, b: { a: 'text', b: 'more_text' }, c: 3 }
db_a:
typeof value =  string
[object Object]

Cheers.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
juliangrubercommented, Jun 17, 2013

The reason db_a returns [object Object] instead of your object is that when you don’t specify valueEncoding: 'json' levelUp will convert whatever value you pass to db#put into a leveldb compatible format by calling val.toString() instead of JSON.stringify(val). If you want to use json values, always pass valueEncoding: 'json'.

0reactions
durancommented, Jun 18, 2013

I will try make some minimal changes to README.md and then make a pull requests. If you dont like it, just reject, no hassle. I know it can be a little fussy and the ‘key’, ‘value’ should be clear enought but I think it can help others.

Regards.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory (Encoding, Storage, Retrieval) - Noba Project
Good encoding techniques include relating new information to what one already knows, forming mental images, and creating associations among information that ...
Read more >
How to change json encoding behaviour for serializable ...
Implementing that could be done by checking for any dict key that is called 'date' and contains a valid string representation of a...
Read more >
Encoding/decoding model of communication - Wikipedia
The Encoding/Decoding model of communication was first developed by cultural studies scholar ... Decoding behavior without using words, displays non verbal communication.
Read more >
Valence encoding in the amygdala influences motivated ...
Valence encoding in the amygdala influences motivated behavior ... between the positive and negative encoding neurons such that presentation ...
Read more >
Controlling the cache key - Amazon CloudFront
Use cache policies to control the cache key for cache behaviors in your ... The cache key doesn't include other values that were...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found