Apify Discord Mirror

Updated 2 years ago

How to pass UserData when executing crawler

At a glance

The community member encountered an ArgumentError when trying to set userData in the options of the crawler.run() method. The comments suggest that userData should be part of the request object, not the options. The correct way to set userData is to pass it within the request object, like this: await crawler.run([{ url: 'https://crawlee.dev', userData: { depth: 0 } }]); Additionally, the community members note that to access the userData in the route handler, you can do const routeHandler = async ({ request, crawler, page, log }) => { let { depth } = request.userData }.

Useful resources
When I do await crawler.run(['https://crawlee.dev'], { userData: { depth: 0 } });
I got this error:
Uncaught ArgumentError ArgumentError: Did not expect property userData to exist, got [object Object] in object options

How can I set userData in option?
`
1
H
A
t
4 comments
await crawler.run([{url: 'https://crawlee.dev', userData: { depth: 0 } }]);
userData is part of request, not part of crawler
You passed the userData as the second parameter of the run() method instead of within the request. Hence the ArgumentError.

Try this instead:
Plain Text
await crawler.run([{
    url: 'https://crawlee.dev',
    userData: {
        depth: 0,
    },
}]);
Also, something that was not obvious to me was how to get the user data on the route.
you can do it like so
Plain Text
const routeHandler = async ({ request, crawler, page, log }) => {
    // get user data
    let { depth } = request.userData  
}
Add a reply
Sign up and join the conversation on Discord