作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 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
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);
});
最佳答案
基本上表明绑定(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.
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/
我是一名优秀的程序员,十分优秀!