gpt4 book ai didi

loopback4 - Loopback 4 命令行脚本 : The key 'repositories.CurrencyRepository' is not bound to any value in context

转载 作者:行者123 更新时间:2023-12-03 23:06:59 25 4
gpt4 key购买 nike

使用 Loopback 4,我试图执行一个简单的 get 请求,之后我想在我的数据库中存储一些数据。

我想从命令行执行以下代码:

import {WebshopApplication} from '../application';
import axios from 'axios';
import {CurrencyRepository} from '../repositories';

export async function importCurrencies(args: string[]) {

const app = new WebshopApplication();
await app.boot();


const host = 'http://data.fixer.io/api';
const accessToken = 'access_key=mykey';

const currencyRepository = await app.getRepository(CurrencyRepository);
const currencies = currencyRepository.find({});
console.log(currencies);

try {

const resp = await axios.get(`${host}/latest?${accessToken}` );
const currencies = resp.data;
console.log(currencies);
} catch (err) {
console.log(err);
}

process.exit(0);
}

importCurrencies(process.argv).catch(err => {
console.error('Cannot import currencies due to error', err);
process.exit(1);
});

当我执行:
 ts-node src/commands/import-currency.command.ts

我收到以下错误:
Cannot import currencies due to error Error: The key 'repositories.CurrencyRepository' is not bound to any value in context WebshopApplication-f9b12a86-ec04-46b4-8e87-4031a4ab71f9

为什么这不起作用?

2020 年 5 月 26 日更新:

根据命令中的建议,我已将上述脚本更新为以下内容。
import {WebshopApplication} from '../application';
import axios from 'axios';
import {CurrencyRepository} from '../repositories';
import {bind, BindingScope} from '@loopback/context';

@bind({scope: BindingScope.TRANSIENT})
export class ImportCurrencies {

generate = async () => {
const app = new WebshopApplication();
await app.boot();

const host = 'http://data.fixer.io/api';
const accessToken ='mytoken';

const currencyRepository = await app.getRepository(CurrencyRepository);
const currencies = currencyRepository.find({});

const resp = await axios.get(`${host}/latest?${accessToken}`);
const currencies = resp.data;

process.exit(0);
}
}

const importCurrencies = new ImportCurrencies();

importCurrencies.generate().catch(err => {
process.exit(1);
});

不幸的是,在类(class)顶部添加绑定(bind)不起作用

最佳答案

基本上表明绑定(bind)键未正确绑定(bind)到应用程序上下文。您是否在 SINGLETON 范围内绑定(bind)了“repositories.CurrencyRepository”?这里有完整的解释https://github.com/strongloop/loopback-next/blob/master/docs/site/Dependency-injection.md#dependency-injection-for-bindings-with-different-scopes

它建议这样做:

@bind({scope: BindingScope.TRANSIENT})
export class someDatasource extends juggler.DataSource {
}

或者

The issue seems to be coming from the @loopback/context. As a temp workaround, use "@loopback/context": "1.5.1" instead of the latest.



RaymondFeng 说这是 Loopback 4 中的绑定(bind)范围问题,请参阅:
https://github.com/strongloop/loopback-next/pull/2513

不同作用域绑定(bind)的依赖注入(inject):
上下文可以形成一个链,绑定(bind)可以在不同的级别注册。绑定(bind)范围不仅控制绑定(bind)值的缓存方式,还控制其依赖关系的解析方式。

可能的故障排除:
import {CurrencyRepository} from '../repositories';--> 路径正确吗? ../?

关于loopback4 - Loopback 4 命令行脚本 : The key 'repositories.CurrencyRepository' is not bound to any value in context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61941346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com