Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON.stringify() possibility in readme #3

Closed
xmik opened this issue Oct 26, 2015 · 5 comments
Closed

Add JSON.stringify() possibility in readme #3

xmik opened this issue Oct 26, 2015 · 5 comments

Comments

@xmik
Copy link
Collaborator

xmik commented Oct 26, 2015

First, thank you for tp-api. The code still works, I'd just like to suggest adding one note to readme.

Using a very similar example to the one from readme:

tp('Tasks')
  .take(1)
  .where("EntityState.Name eq 'Open'")
  .pluck("CustomFields")
  .sortBy('NumericPriority')
  .then(function(err, tasks) {
    console.log('my tasks', tasks)
  }
)

The returned tasks here is a JavaScript object which can return:

my tasks [ { ResourceType: 'Task',
    Id: 3631,
    Project: { ResourceType: 'Project', Id: 4026, Process: [Object] },
    CustomFields: [ [Object], [Object], [Object], [Object], [Object] ] } ]

In order to convert it into JSON string, the following is enough:

tp('Tasks')
  .take(1)
  .where("EntityState.Name eq 'Open'")
  .pluck("CustomFields")
  .sortBy('NumericPriority')
  .then(function(err, tasks) {
    console.log('my tasks', JSON.stringify(tasks))  // changed here
  }
)

and now it returns:

my tasks [{"ResourceType":"Task","Id":3631,"Project":{"ResourceType":"Project","Id":4026,"Process":{"Id":5,"Name":"AIScrum"}},"CustomFields":[{"Name":"Component","Type":"DropDown","Value":null},{"Name":"trac","Type":"Number","Value":null},{"Name":"Job","Type":"DropDown","Value":null},{"Name":"Resolution","Type":"DropDown","Value":null},{"Name":"Domain","Type":"DropDown","Value":null}]}]

It took me some time to find out about it, so I'd like to spare this time for others.

@8bitDesigner
Copy link
Owner

Good call; I'll be honest and say that we've moved to using Jira at Fullscreen, so I don't have any way to confirm or test this. Can I give you contributor permissions so you can commit this change directly to the repo?

@xmik
Copy link
Collaborator Author

xmik commented Oct 26, 2015

That would be ok. I also have 2 new methods to add (to append fields and sort in descending order described on dev.targetprocess.com). But I have to admit, I don't have a lot of experience with JavaScript.

@8bitDesigner
Copy link
Owner

Well, you have commit access. Feel free to open a pull request and ping me if you need a second set of eyes, or a hand walking through my terrible, terrible code.

@xmik
Copy link
Collaborator Author

xmik commented Oct 26, 2015

Thanks

@xmik xmik closed this as completed in 487d659 Oct 26, 2015
xmik added a commit that referenced this issue Oct 26, 2015
Fixes #3 Add JSON.stringify() possibility in readme; and a typo
@mathiasschopmans
Copy link

mathiasschopmans commented Oct 26, 2016

@xmik The problem is related to console.log as it's not printing all levels of the object.

I prefer to use node's util.inspect to inspect variables, as you do have more fine-grained control. :)

const util = require('util');
console.log(util.inspect(YOUR_OBJECT, { showHidden: true, depth: null }));

From the docs:

The util.inspect() method returns a string representation of object that is primarily useful for debugging. Additional options may be passed that alter certain aspects of the formatted string.

So this can be easily adapted to your example:

var util = require('util');
tp('Tasks')
  .take(1)
  .where("EntityState.Name eq 'Open'")
  .pluck("CustomFields")
  .sortBy('NumericPriority')
  .then(function(err, tasks) {
    console.log(util.inspect(tasks, { showHidden: true, depth: null }));
  }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants