Rishikant181 / Rettiwt-API

A CLI tool and an API for fetching data from Twitter for free!
https://rishikant181.github.io/Rettiwt-API/
MIT License
483 stars 46 forks source link

Cannot read properties of undefined (reading 'created_at') #620

Closed hst-Sunday closed 1 month ago

hst-Sunday commented 1 month ago

The program was working fine yesterday, but today this error keeps showing up.

/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/models/data/Tweet.js:21
        this.createdAt = tweet.legacy.created_at;
                                      ^

TypeError: Cannot read properties of undefined (reading 'created_at')
    at new Tweet (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/models/data/Tweet.js:21:39)
    at new Tweet (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/models/data/Tweet.js:25:52)
    at new Tweet (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/models/data/Tweet.js:36:15)
    at Tweet.list (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/models/data/Tweet.js:59:29)
    at new CursoredData (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/models/data/CursoredData.js:28:39)
    at Object.LIST_TWEETS (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/collections/Extractors.js:16:16)
    at TweetService.<anonymous> (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/services/public/TweetService.js:232:65)
    at step (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/services/public/TweetService.js:59:23)
    at Object.next (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/services/public/TweetService.js:40:53)
    at fulfilled (/Users/node_modules/.pnpm/rettiwt-api@4.1.1/node_modules/rettiwt-api/dist/services/public/TweetService.js:31:58)
Rishikant181 commented 1 month ago

Try the same with logging enabled and post the output.

hst-Sunday commented 1 month ago
[Rettiwt-API] [2024-09-25T15:36:50.336Z] [REQUEST] {"resource":"LIST_TWEETS","args":{"id":"1838967462660112854","count":10}}
[Rettiwt-API] [2024-09-25T15:36:50.338Z] [AUTHORIZATION] {"authenticated":true}
[Rettiwt-API] [2024-09-25T15:36:50.338Z] [VALIDATE] {"target":"FETCH_ARGS"}
[Rettiwt-API] [2024-09-25T15:36:50.339Z] [GET] {"target":"HTTPS_AGENT"}
[Rettiwt-API] [2024-09-25T15:36:50.339Z] [GET] {"target":"USER_CREDENTIAL"}
[Rettiwt-API] [2024-09-25T15:36:53.303Z] [DESERIALIZE] {"id":"1838864011321872407"}
[Rettiwt-API] [2024-09-25T15:36:53.303Z] [DESERIALIZE] {"id":"1838817346766684408"}
[Rettiwt-API] [2024-09-25T15:36:53.304Z] [DESERIALIZE] {"id":"1838816531347836957"}

@Rishikant181 This is the output after the logging is enabled

Rishikant181 commented 1 month ago

Okay so the tweet on which this error occurs exists and is valid. Can you supply me with the method and arguments you used which threw this error?

hst-Sunday commented 1 month ago

const ret = await rettiwt.tweet.list("1838967462660112854", 10);

Rishikant181 commented 1 month ago

Got the origin of the issue. The problem is the strategy used to identifying and extracting the tweets from the raw response. I'm testing alternative strategies now.

tjanczuk commented 1 month ago

@Rishikant181 Is the fix on the horizon, or is there a workaround? I am getting the same issue with the search API.

Rishikant181 commented 1 month ago

@tjanczuk I've been a bit busy to couldn't find the time to work on this. As for the issue with the search API, I think it's because of a different reason than the one in list API. Can you provide me the query which threw this error? Would help me reproducing the issue on my end.

tjanczuk commented 1 month ago

Thanks! The query looks something like this:

await client.tweet.search({ 
  includeWords: ["(#ClimateChange OR #NetZero OR #ESG OR #ClimateAction OR #CarbonEmissions OR #GreenEnergy OR #ClimateTech OR #CSRD OR #COP28) -is:retweet"],
  startDate: new Date(Date.now() - 24 * 60 * 60 * 1000)
}, 20);

The issue is intermittent - I suppose it may depend on the actual result of the search request. But the error is the same:

TypeError: Cannot read properties of undefined (reading 'created_at')
    at new Tweet (/Users/.../node_modules/rettiwt-api/src/models/data/Tweet.ts:76:33)

On a separate note, it would be nice to be able to pass the Twitter query string directly to the search API, without having to restate it as TwitterFilter. The use of includeWords with a single-member array works for that purpose but feels more like a workaround.

Rishikant181 commented 1 month ago

I suppose it may depend on the actual result of the search request

Yup, you're right on that. What I'm currently doing is that I'm filtering all items which have their __typename field set to Tweet. But sometimes, there maybe nested threads which might throw out an error. Anyways, I'm the exact erroneous item.

it would be nice to be able to pass the Twitter query string directly to the search API

I've been trying to provide alternate ways to interact with the API in a more raw form or perhaps a form similar to that of official API. In that direction, I've already made it such that you can get raw response data instead of processed data by using the FetcherService class. However, as for search query, there is no such mean as of now, but rest assured, I'll try to implement it. The reason TweetFilter exists is because it encapsulates some validations which are help in the sense that any invalid args are taken care of before the request is actually made to Twitter, thereby saving a single wasted HTTP request.

tjanczuk commented 1 month ago

Great! To get raw responses, do I call FetcherService.request<T> directly?

Rishikant181 commented 1 month ago

@tjanczuk Here is the documentation with an example to help you get started.

matgott commented 1 month ago

Hi, I got into this same problem with the search method. Looks like sometimes the Tweet class (rettiwt-api\models\data\Tweet) is instantiated with a object with __typename = '"TweetWithVisibilityResults"' that insde has a tweet property with the actual tweet:

image

Rishikant181 commented 1 month ago

@matgott Yes I noticed that too. I'm working on it rn.

Rishikant181 commented 1 month ago

Issue has been fixed. Please upgrade rettiwt-api to v4.1.2.

Rishikant181 commented 1 month ago

There was a "Maximum call stack exceeded" error while deserializing tweet(s) in v4.1.2.

Please upgrade to v4.1.3 instead.