How to access Actor input inside route handlers?
Hello, I am having trouble figuring out how to do the following, and cannot find anything in the docs:
Suppose I want to use variables provided in my input_schema, ex:
Inside of my route handlers, ex
How can I access input variables inside of my route handlers?
13 Replies
correct-apricot•15mo ago
You can pass input via userData (https://crawlee.dev/api/core/interface/RequestOptions#userData)
And then get it in Your handler like this:
const { input } = ctx.request.userData;
correct-apricotOP•15mo ago
Will crawler.run accept the dict format for startUrls vs raw strings?
const crawler = new PlaywrightCrawler({
proxyConfiguration,
maxRequestsPerCrawl,
navigationTimeoutSecs,
requestHandler: router,
});
await crawler.run(startUrls);
correct-apricot•15mo ago
Here are docs with all types:
https://crawlee.dev/api/playwright-crawler/class/PlaywrightCrawler#run
requests: (string | Request<Dictionary> | RequestOptions<Dictionary>)[]
correct-apricotOP•15mo ago
Thanks for the info. I am still having trouble. When providing a valid RequestOptions object into the start urls array and passing it to crawler.run, request.userData is still an empty dict.
Main.ts:
routes.ts
Is there another way to achieve this with useState? I cannot find any examples which show how to define state in main.ts then use the defined state in route.ts
Edit: The issue was related to me importing a type defined in my main.ts file in route.ts. I am able to get this working now. Thanks!
I am still confused as to how I can define state using useState in main.ts, then access this state in my route.ts
this approach would actually be preferred for me since I want to be able to set global configuration that I can reference in state in each of my route handlers. I attempted to define the state in my main.ts file
`export const state = useState("socialsConfig:", socialsConfig)'
But how can I use this state in my route handlers in route.ts? I tried importing state, but this casued a cyclic dependency.
correct-apricot•15mo ago
You don't need to export it from main.
Here is working example:
correct-apricotOP•15mo ago
I copied this code verbatem, and playwrightDetails is undefined in my route.ts
@sunlover just advanced to level 2! Thanks for your contributions! 🎉
correct-apricotOP•15mo ago
Do you know why this may be?
Here is my exact code
main.ts
routes.ts
Nomatter what I do, the state in routes.ts is always undefined
correct-apricot•15mo ago
You should move
const { playwrightDetails } = await useState();
to DefaultHandler
correct-apricotOP•15mo ago
Thanks I got it working @Oleg V. 🙏
ambitious-aqua•2mo ago
why is that hard to find this informations on crawlee docs
@Scai just advanced to level 2! Thanks for your contributions! 🎉
ambitious-aqua•2mo ago
I've got it to working as the way I wanted.