I use cronjob that described in document and i whould use repository in job but below error occured
this is my CronJobComponent:
import {CronJob, cronJob} from '@loopback/cron';
import {repository} from '@loopback/repository';
import moment from 'moment';
import {SettingsRepository, UsersRepository} from '../repositories';
@cronJob()
export class CronJobComponent extends CronJob {
constructor(
@repository(UsersRepository) public usersRepository: UsersRepository,
@repository(SettingsRepository)
public settingsRepository: SettingsRepository,
) {
super({
name: 'job-1',
cronTime: '* */1 * * * *',
onTick: async () => {
await this.scheduleNotifyJob();
},
start: false,
});
}
async scheduleNotifyJob() {
const foundUserIds = await this.settingsRepository.find({
fields: {userId: true},
where: {
scheduleNotify: true,
scheduleTime: moment().startOf('minute').toString(),
},
});
console.log(foundUserIds);
}
}
This is my Application.ts
import {CronJobComponent} from './components';
import {
ApplicationConfig,
BindingKey,
createBindingFromClass,
} from '@loopback/core';
export class MyApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
hashRound: number;
constructor(options: ApplicationConfig = {}) {
...
this.component(CronJobComponent);
this.add(createBindingFromClass(CronJobComponent));
...
}
This issue occured:
Cannot start the application. ResolutionError: The key 'repositories.SettingsRepository' is not bound to any value in context MyApplication-4yxpOGOFSwS3Y68UCUEVGA-0 (context: MyApplication-4yxpOGOFSwS3Y68UCUEVGA-0, binding: repositories.SettingsRepository, resolutionPath: components.CronJobComponent --> @CronJobComponent.constructor[0])
at MyApplication.getValueOrPromise (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/context.ts:810:13)
at /home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolver.ts:159:20
at /home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolution-session.ts:157:13
at tryCatchFinally (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/value-promise.ts:222:14)
at Object.tryWithFinally (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/value-promise.ts:197:10)
at Function.runWithInjection (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolution-session.ts:156:12)
at resolve (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolver.ts:143:38)
at /home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolver.ts:246:12
at Object.resolveList (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/value-promise.ts:169:28)
at resolveInjectedArguments (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolver.ts:224:10)
at Object.instantiateClass (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolver.ts:62:25)
at /home/navid/Projects/dongip_services/node_modules/@loopback/context/src/binding.ts:747:29
at Binding._getValue (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/binding.ts:603:14)
at /home/navid/Projects/dongip_services/node_modules/@loopback/context/src/binding.ts:478:23
at /home/navid/Projects/dongip_services/node_modules/@loopback/context/src/resolution-session.ts:122:13
at tryCatchFinally (/home/navid/Projects/dongip_services/node_modules/@loopback/context/src/value-promise.ts:222:14) {
resolutionCtx: {
context: MyApplication {
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: Infinity,
registry: [Map],
_parent: undefined,
name: 'MyApplication-4yxpOGOFSwS3Y68UCUEVGA-0',
tagIndexer: [ContextTagIndexer],
subscriptionManager: [ContextSubscriptionManager],
_debug: [Function],
_isShuttingDown: false,
_state: 'created',
options: [Object],
_shutdownOptions: [Object],
hashRound: 10,
configResolver: [DefaultConfigurationResolver],
[Symbol(kCapture)]: false
},
binding: Binding {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
isLocked: false,
tagMap: {},
key: 'repositories.SettingsRepository',
[Symbol(kCapture)]: false
},
options: {
session: [ResolutionSession],
decorator: '@inject',
name: 'SettingsRepository',
modelName: undefined,
modelClass: undefined,
dataSourceName: undefined,
dataSource: undefined
}
},
name: 'ResolutionError'
}
node -e 'console.log(process.platform, process.arch, process.versions.node)'
linux x64 12.16.2
```
npm ls --prod --depth 0 | grep loopback
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── [email protected]
```
Sorry for reporting in bug. now i noticed and cannot move to questions
@navidarry, i just created an app with CronJob, which is similar to yours. I think in application.ts, it should be:
import {CronComponent} from '@loopback/cron';
and in the constructor:
this.component(CronComponent); //CronComponent from the cron package
this.add(createBindingFromClass(MyCronJob)); //MyCronJob is the component extends CronJob
Not sure if that's the cause of the problem. Could you please give it a try?
Thanks for respnd but not solved my issue
@navid-ahrary, I just push my app in https://github.com/dhmlau/cron-test. Could you please take a look?
YYYYESSSSSSS.
That's solved by your solution
tnx soooooo