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

track().promise and flush().promise never resolves #737

Closed
oluckyman opened this issue May 6, 2024 · 3 comments
Closed

track().promise and flush().promise never resolves #737

oluckyman opened this issue May 6, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@oluckyman
Copy link

oluckyman commented May 6, 2024

Expected Behavior

I expect await track|flush().promise to resolve

Current Behavior

It awaits forever.

Possible Solution

I nailed down the issue is in transports/http:

          if (res.complete && responsePayload.length > 0) {
            try {
              // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
              var parsedResponsePayload = JSON.parse(responsePayload);
              var result = _this.buildResponse(parsedResponsePayload);
              console.log("RESOLVING RESULT!", result);
              resolve(result);
              return;
            } catch (_a) {
              resolve(null);
            }
          }
        });

This code never runs because res.complete is false, even though the server returns the correct result and registers the event:

{"code":200,"server_upload_time":1714999800311,"payload_size_bytes":274,"events_ingested":1}

Steps to Reproduce

import { flush, init, track, Types } from '@amplitude/analytics-node'
const client = init('my-key')
const logEvent: async () => {
    track('command', { command: 'bla bla' }, {
      user_id: userId,
    }).promise.then((res) => {
      // this func never runs
      return res
    })
    const f = flush()
    console.log(f.promise) // shows Promise { <pending> }
    console.log('now lets wait')
    const res = await f.promise
    console.log('got it', res) // never arrives to this line
}

Environment

// deno.json
{
  "imports": {
    "@amplitude/analytics-node": "npm:@amplitude/[email protected]"
  }
}
  • Browser and Version: server environment
@oluckyman oluckyman added the bug Something isn't working label May 6, 2024
@oluckyman oluckyman changed the title track().promise and flush().promise never returns track().promise and flush().promise never resolves May 6, 2024
@sosafe-ugonna-ofoegbu
Copy link

sosafe-ugonna-ofoegbu commented May 30, 2024

Currently having similar issue with identify().promise using package analytics-browser.
It never resolves.
Probably something related to changes made here

export const convertProxyObjectToRealObject = <T>(instance: T, queue: QueueProxy): T => {

@Mercy811
Copy link
Contributor

Hi @oluckyman,

You are missing await when track. I've changed

with the following and tested that it should work as expected

@Get('track')
  async track(): Promise<string> {
    await amplitude.track(
      'nest-app example event',
      { property1: '1' },
      { user_id: 'test_user' },
    ).promise.then(() => {
      console.log('Event sent successfully');
    }).catch((error) => {
      console.error('Error sending event:', error);
    });

    // Flushing the events
    const f = amplitude.flush();
    console.log(f.promise); // shows Promise { <pending> }
    console.log('now lets wait');

    const res = await f.promise;
    console.log('got it', res); // should log the flush result

    return 'Triggered, check console output...';
  }

Output:

Amplitude Logger [Log]: Event tracked successfully
Amplitude Logger [Debug]: {
  "type": "invoke public method",
  "name": "track",
  "args": [
    "nest-app example event",
    {
      "property1": "1"
    },
    {
      "user_id": "test_user"
    }
  ],
  "stacktrace": [
    "at AppController.track (/Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/src/app.controller.ts:23:21)",
    "at /Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/@nestjs/core/router/router-execution-context.js:38:29",
    "at InterceptorsConsumer.intercept (/Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:11:20)",
    "at /Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/@nestjs/core/router/router-execution-context.js:46:60",
    "at /Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/@nestjs/core/router/router-proxy.js:9:23",
    "at Layer.handle [as handle_request] (/Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/express/lib/router/layer.js:95:5)",
    "at next (/Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/express/lib/router/route.js:144:13)",
    "at Route.dispatch (/Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/express/lib/router/route.js:114:3)"
  ],
  "time": {
    "start": "2024-06-24T21:09:37.187Z",
    "end": "2024-06-24T21:09:47.385Z"
  },
  "states": {
    "before": {
      "config.apiKey": "5b9a9510e261f9ead90865bbc5a7ad1d",
      "timeline.queue.length": 0
    },
    "after": {
      "config.apiKey": "5b9a9510e261f9ead90865bbc5a7ad1d",
      "timeline.queue.length": 0
    }
  }
}
Event sent successfully
Promise { <pending> }
now lets wait
Amplitude Logger [Debug]: {
  "type": "invoke public method",
  "name": "flush",
  "args": [],
  "stacktrace": [
    "at AppController.track (/Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/src/app.controller.ts:34:25)",
    "at processTicksAndRejections (node:internal/process/task_queues:95:5)",
    "at /Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/@nestjs/core/router/router-execution-context.js:46:28",
    "at /Users/xinyi.ye/Documents/GitHub/Amplitude-TypeScript/examples/node/nest-app/node_modules/@nestjs/core/router/router-proxy.js:9:17"
  ],
  "time": {
    "start": "2024-06-24T21:09:47.387Z",
    "end": "2024-06-24T21:09:47.389Z"
  },
  "states": {
    "before": {
      "config.apiKey": "5b9a9510e261f9ead90865bbc5a7ad1d",
      "timeline.queue.length": 0
    },
    "after": {
      "config.apiKey": "5b9a9510e261f9ead90865bbc5a7ad1d",
      "timeline.queue.length": 0
    }
  }
}
got it undefined

@Mercy811
Copy link
Contributor

Mercy811 commented Jul 9, 2024

Close this issue as not hearing back.

@Mercy811 Mercy811 closed this as completed Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants