(Node.js Asana) getStatus not working

I’m running into a wall here, I’m trying to get a project’s status via this

when I do it in the browser(in the above link), everything works fine, but when I try doing it in my IDE (visual studio code), even with asana’s library installed (npm install asana), I’m getting this:

TypeError: Cannot read properties of undefined (reading ‘getStatus’)

this is the code sample I’m using that I took from the dev doc

const asana = require(‘asana’);

const client = asana.Client.create().useAccessToken(‘ValidAccessTokenHere’);
let statusGid = “ValidStatusGIDHere”;
client.statusupdates.getStatus(statusGid, {opt_fields: “status_type”, opt_pretty: true})
.then((result) => {
console.log(result);
});

Am I doing something wrong?

Thanks!

In a case like this I always check the source code to see if this method is really named this way. Did you?

Thanks for your response Bastien,

I’m basing myself on the dev website for the method syntax

From what I understand, this syntax is still based on Asana 1.0.2, which the rest of my code is built on.

If I check the asana package, they have usage samples that basically mirror what the dev site says.

As for the source code, the only thing I found regarding statuses was this:

ProjectStatuses.prototype.findById = function(
    projectStatus,
    params,
    dispatchOptions
) {
    var path = util.format('/project_statuses/%s', projectStatus);

    return this.dispatchGet(path, params, dispatchOptions);
};

but to be frank I have no idea how to call it, I’ve tried

client.ProjectStatuses.findById()
&
client.ProjectStatuses.getProjectStatus() to no avail

TypeError: Cannot read properties of undefined (reading ‘getProjectStatus’)
TypeError: Cannot read properties of undefined (reading ‘findById’)

I apologize if I’m missing something obvious

The syntax in the V1 client is client.projectStatuses I believe, not sure how I know though

Hi @Stephane_Caron,

The reason why you are not able to call

client.statusupdates.getStatus(statusGid, {param: "value", param: "value", opt_pretty: true})

Is because the asana node client doesn’t know about this getStatus method or the statusupdates class even though the getStatus method exists in the v1.0.2 library.

This probably happened because our v1 library relies on a mix of auto generation + manual editing to get new resource endpoints working. We’re working on a v2 of our node-asana client library which should help us with keeping our client library up-to-date with our API. (v2 is in Beta right now at v2.X.X)

Here’s a quick explanation and example of why one method works vs why this one doesn’t.

WORKING (EX: tasks.getTask):

  1. The getTask method is defined in /gen/tasks.js (this file was auto generated by our code generator)
  2. In the /resources folder, there is a file for tasks.js which imports the generated method definitions from /gen/tasks.js
  3. The client.js knows about tasks because this.tasks = new resources.Tasks(this.dispatcher); is defined → this is why you can call client.tasks

NOT WORKING (EX: statusupdates.getStatus):

  1. The getStatus method is defined in /gen/status_updates.js (this file was auto generated by our code generator)
  2. In the /resources folder, it is missing a file for status_updates.js which should import the auto generated methods defined in the /gen/status_updates.js file
  3. The client.js is missing a a definition for this.statusupdates = new resources.StatusUpdates(this.dispatcher); → this is what I meant by the client not knowing about statusupdates

TLDR; this is a bug with the v1 node library.

The workaround is to use our dispatcher method that is in our v1.0.2 node-asana library. You can use this to call any API endpoints that the v1.0.2 library does not have define. Here’s an example of how you would make a request to getStatus:

const asana = require('asana');

const client = asana.Client.create().useAccessToken("<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>");

statusGid = "<YOUR_STATUS_GID>"
path = `/status_updates/${statusGid}`

client.dispatcher.get(path, {}, {}).then((data) => {
    console.log(JSON.stringify(data, null, 2));
})

Here’s the definition for that dispatcher.get method:

        /**
         * Dispatches a GET request to the Asana API.
         * @param  {String}  path    The path of the API
         * @param  {Object}  [query] The query params
         * @param  {Object}  [dispatchOptions] Options for handling the request and
         *     response. See `dispatch`.
         * @return {Promise}         The response for the request
         * @param path
         * @param query?
         * @param dispatchOptions?
         * @return
         */
        get(path: string, query?: any, dispatchOptions?: any): Promise<any>;
2 Likes

Thank you so much for your well written explanation, it is very much appreciated!

The work around I came about is more or less similar to yours, when I noticed that status_updates.js still existed but wasn’t defined/exported.

I achieve this by adding the following lines in the asana node_modules

  1. exports.StatusUpdates = require('./gen/status_updates'); in asana/lib/resources/index.js

  2. this.StatusUpdates = new resources.StatusUpdates(this.dispatcher); in asana/lib/client.js

Do you believe that this could be a long term solution? Or will I get caught in depreciation hell eventually? :sweat_smile:

Thanks a lot John and have a nice day

1 Like

Hi @Stephane_Caron,

I think your solution works but I don’t think that is good practice. I am not a Node/JavaScript expert but it seems to me you are modifying the asana package in the node_modules itself which gets created when you do an npm install which installs the dependencies you listed in package.json. Normally, you do not want to commit the node_modules folder to any version control and would tell collaborators or if you are hosting on a server to do an npm install which will install the dependencies you specified in package.json but when this happens your collaborators/server won’t have your modifications.

Here’s what a quick Google search of “should you modify packages in node_modules” says:

Don’t change code in node modules

Of course it is advisable never to touch the code in your node modules folder, for one thing all your changes will be overwritten when you run npm install (or yarn) and no-one else will be able to replicate when they use your repo as you don’t check node modules into git.

1 Like

Indeed changing the node_modules file is a bad practice, or at least a bad idea as it will be erased with the next install. That being said, I had to do it a few years ago because Asana was not allowing the nodeJS client to be used inside the browser. A wrote a script replacing the node_modules file I was targeting with my own version. (I called it asana-repair.sh ^^)

1 Like

Yeah I agree, it’s not a great idea, but for the scope of this project (I’m just personally running this code locally from my machine every now and then) I think it would be adequate, I’ll just need to keep myself notes of the changes needed if I ever need to reinstall Asana.

For anyone that has a more implicated use-case your solution seems definitively more apt.

Thanks a lot John!

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