Я использую cronjob, описанный в документе, и я должен использовать репозиторий в задании, но ниже произошла ошибка, это мой 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);
}
}
Это мой Application.ts файл
export class MyApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
this.component(CronJobComponent);
this.add(createBindingFromClass(CronJobComponent));
}
}
Текущее поведение
Это вывод, в котором упоминается:
Ключ 'repositories.SettingsRepository' не привязан ни к какому значение в контексте
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])
{
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'
}
Дополнительная информация о моей среде:
linux x64 13.13.0
├── @loopback/authentication@4.2.10
├── @loopback/authorization@0.6.3
├── @loopback/boot@2.4.0
├── @loopback/cli@2.11.0
├── @loopback/context@3.9.3
├── @loopback/core@2.9.2
├── @loopback/cron@0.2.10
├── @loopback/express@1.2.6
├── @loopback/extension-health@0.4.10
├── @loopback/extension-metrics@0.3.10
├── @loopback/openapi-v3@3.4.6
├── @loopback/repository@2.10.0
├── @loopback/rest@5.2.1
├── @loopback/rest-explorer@2.2.7
├── @loopback/service-proxy@2.3.5
├── loopback-connector-mysql@5.4.3