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.

Get / Set JSON Value?

See original GitHub issue

I’m having some issues when attempting to set and get a JSON value, currently I have some code similar to the following:

      memcache.get(username, function(err, value, key) {
        var query, siteCode, user, userCredentials;
        if (err != null) {
          winston.error("BasicAuth: Error occured when attempting to search memcache for the user, error follows: " + (util.inspect(err)));
          return done(err);
        }
        if (value != null) {
          user = value.toJSON();
          if (passwordHash.verify(password, user.hashedPassword)) {
            winston.info("BasicAuth: User " + username + " with siteCode " + siteCode + " successfully logged in");
            return done(null, user);
          } else {
            return done(null, false, {
              message: 'Invalid username/password combination'
            });
          }
        }
        memcache.set(username, { some: 'JSON value', with: 'A Few', arrays: [] }, function(err, success) {
          if (err != null) {
            winston.error("BasicAuth: Failed to store the users details in memcache, error follows: " + (util.inspect(err)));
            return done(null, userDetails);
          }
          winston.info("BasicAuth: Successfully stored user's details in the memcache server");
          winston.info("BasicAuth: User " + username + " with siteCode " + siteCode + " successfully logged in");
          return done(null, {});
        })
      }

But when I call the get and then user = value.toJSON(); I get a zero length array!?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
coderholiccommented, May 10, 2016

I’ve written a simple wrapper that handles this nicely:

memjs = require('memjs')

class SimpleCache
    constructor: () ->
        @cache = memjs.Client.create(null, {expires: 24 * 60 * 60})

    get: (key, callback) ->
        @cache.get key, (err, value) ->
            return callback(err) if err
            value = value.toString()
            try
                value = JSON.parse(value)
            catch e
                null
            callback(null, value)

    set: (key, value, callback) ->
        @cache.set key, JSON.stringify(value), callback

    del: (keys, callback) ->
        @cache.del keys, callback

module.exports = new SimpleCache()
4reactions
Siyfioncommented, Mar 28, 2014

@alevy I got there in the end, I realised that I needed to call JSON.stringify when setting and then JSON.parse(buffer.toString()) when getting the data. But I agree, an example would have been nice! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Object Literals - W3Schools
JSON object literals contains key/value pairs. Keys and values are separated by a colon. Keys must be strings, and values must be a...
Read more >
How to Get Value from JSON Object in Java Example
It is used to get the (JsonString)get(name). The method parses an argument name of type String whose related value is to be returned....
Read more >
c# get values from json - Stack Overflow
var data = (JObject)JsonConvert.DeserializeObject(myJSON); string nameArticles= data["articles"].Value<string>(); MessageBox.
Read more >
Working with JSON - Learn web development | MDN
In this article, we've given you a simple guide to using JSON in your programs, including how to create and parse JSON, and...
Read more >
Access and print a specific JSON value | Documenting APIs
Getting a specific property from a JSON response object; Printing a JSON value to the page; Get the value from an array; More...
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