Retrieve all projects in express

I am trying to retrieve all projects in express, and this is my code.
var Asana = require(‘asana’);
var client = Asana.Client.create({
clientId: ,
clientSecret:
,
redirectUri: ‘http://localhost:8000
}).useAccessToken(‘*****’);
console.log(client.projects);

When I print client.projects, it only shows
Projects {
dispatcher:
Dispatcher {
authenticator: OauthAuthenticator { credentials: [Object], flow: null, app: undefined },
asanaBaseUrl: ‘https://app.asana.com/’,
retryOnRateLimit: false,
handleUnauthorized: [Function],
_cachedVersionInfo: null,
requestTimeout: null } }

So how can i retrieve all the projects? Thanks.
If possible, please show me any related information, thanks a lot!

1 Like

Your authentication process went smoothly and Asana sent the user back to your app with a token?

1 Like

Hi Bastien, thanks for your reply. Do you mean this?
when i try this code:
client.users.me().then(function (user) {
console.log(user);
})
I can get the user id, email, name, photo, and workspaces after print the “user”.
And I need to retrieve all the projects from this workspace. What kinds of syntax I can use? And where can I check those syntax from documentation? I have read the github code(GitHub - Asana/node-asana: Official node.js and browser JS client for the Asana API v1) but I didn’t see it.
Thanks.

1 Like

Hey @Jiahao_Yu,

Thanks for reaching out. To get all projects you use the function findAll(), which requires at least one param (e.g. workplace, team, archived).

Here’s an example of getting all projects in a workspace using the Node client library:

client.projects.findAll({"workspace":123456789})
	.then(function(response) {
 		console.log(response);
 	})

You can find the code to get all projects on line 160 of projects.js in the Asana Node client library.

Cheers!
Jeff

2 Likes

@Jeff_Schneider Thank you very much! That’s what I need!

1 Like

@Jeff_Schneider Hi Jeff, I have one more question about this.
I am trying to retrieve one specific project, after read the documentation, I try this code:
client.projects.findById({ “project”: 12345678}).then(function (response) {
console.log(response);
})
but it doesn’t work.
In the documentation,
Projects.prototype.findById = function(
project,
params,
dispatchOptions
) {
var path = util.format(‘/projects/%s’, project);

return this.dispatchGet(path, params, dispatchOptions);
};
How can i make the correct format of the params? Thanks a lot.

1 Like

Hey @Jiahao_Yu,

You just need to pass the project_id as a param:

client.projects.findById(123456789)
	.then(function(response) {
		console.log(response);
	})
1 Like

Hello @Jeff_Schneider,

Firstly thanks for the answer you provided, I had the same problem as Jiahao and now I’m getting all projects correctly. However I was wondering why when using:

.then(function(response) {
		console.log(response);
	})

I am getting both “collection data” with the list of projects and “response data” which is composed of a repetition of "{Object}, {Object}, … ? Is it better to always use “collection” instead of “response” ? Why am I getting this list ?

Thanks,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.