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.

Can't select object properties to serialize

See original GitHub issue

I have data in my Mongo DB that looks like this:

[
  {
    role: 'admin',
    local: {
      password: '___password_hash___',
      email: 'admin@example.com'
    }
  },
  {
    role: 'standard',
    local: {
      password: '___password_hash___',
      email: 'standard@example.com'
    }
  }
]

I want to write a serializer that excludes the password property (for obvious reasons), so that each user object would look like this:

{
  role: 'admin',
  local: {
    email: 'admin@example.com'
  }
}

I’ve tried this as a serializer:

const JSONAPISerializer = require('jsonapi-serializer').Serializer;

module.exports = new JSONAPISerializer('user', {
  attributes: ['role', 'local'],
  local: {
    attributes: ['email']
  },
  pluralizeType: false,
  keyForAttribute: 'camelCase'
});

My understanding is that the attributes: ['email'] line specifies the list of attributes in the local object that should be returned – and that no other attributes will be returned.

However, even using that serializer, I still get this returned:

{
  "data": [
    {
      "type": "user",
      "id": "587e6c7a62af4891d801b0c8",
      "attributes": {
        "role": "admin",
        "local": {
          "email": "admin@example.com",
          "password": "___password_hash___"
        }
      }
    },
    {
      ...
    }
]

Help! What should I be doing? I must be missing something small.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
SeyZcommented, Jan 23, 2017

@mcalthrop you can also use lean() function in Mongoose to have directly plain JSON instead of Mongoose documents 😉

0reactions
mcalthropcommented, Jan 23, 2017

@SeyZ: ah, thanks for that… it’s simplified my code. Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to Json.Serialize an array of objects inside a ViewModel
var xyz = @Json.Serialize(Model.Select(x => x.PropName).ToArray());. However, I have a model where an object array is ...
Read more >
Serialize C# Properties (how-to with code) - Unity Forum
Hi all! There's one particular C# feature I use a lot in my projects, and that is properties: They allow you to execute...
Read more >
4 ways to make a property deserializable but not serializable ...
Approach 1: Use attributes JsonProperty together with JsonIgnore Below is the sample code: · Approach 2: Add ShouldSerialize method. Newtonsoft.
Read more >
How to serialize properties of derived classes with System ...
In this article, you will learn how to serialize properties of derived classes with the System.Text.Json namespace.
Read more >
Property form - Completing the General tab for Page modes
The concrete class for the embedded page cannot differ from the class recorded here. ... Select Java Page to enable the Serialize this...
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