How can I use $fetch in nuxt-cron?
bobrov-site opened this issue · comments
Hello! Can you help me to understand how I can use $fetch inside cron?
fill path = server/cron//bnovo-auth.ts
import { defineCronHandler } from '#nuxt/cron';
export default defineCronHandler('everyTenMinutes', () => {
$fetch('/bnovo/auth')
.then(() => {
console.log('bnovo auth success')
})
.catch((e) => {
console.log('bnovo auth failed')
})
}, {runOnInit: true})
I understood that, if I want to use $fetch I need nitro event handler, like this example https://nitro.unjs.io/guide/fetch#in-server-fetch
But I need to use your cron with my api. For this solution I prefer use $fetch, because it's working on server side. But in first time running script I have next error:
[nuxt] [request error] [unhandled] [500] Cannot access 'nitroApp' before initialization
And this is okay for this.
I will glad if you help me with this problem.
For more understanding I provide to you my code from repository.
this is my cron file
https://github.com/bobrov-site/nedvijka/blob/087d4b7d7017df60f70eb08f4b5380418d08f594/server/cron/bnovo-auth.ts#L4
this is my auth file, when i call my bnovo auth
https://github.com/bobrov-site/nedvijka/blob/master/server/routes/bnovo/auth.ts