Loopback 4 命令行脚本:键 'repositories.CurrencyRepository' 未绑定到上下文中的任何值
Loopback 4 Command line script: The key 'repositories.CurrencyRepository' is not bound to any value in context
使用 Loopback 4,我尝试执行一个简单的获取请求,之后我想将一些数据存储在我的数据库中。
我想从命令行执行以下代码:
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
为什么这不起作用?
更新 26-05-2020:
我已根据命令中的建议将上述脚本更新为以下内容。
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 的顶部添加绑定不起作用
基本上表示绑定键未正确绑定到应用程序上下文。您是否在 SINGLETON 范围内绑定了 '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 中的绑定范围问题,请参阅:
https://github.com/strongloop/loopback-next/pull/2513
不同范围绑定的依赖注入:
上下文可以形成一条链,绑定可以在不同级别注册。绑定范围不仅控制绑定值的缓存方式,还控制其依赖项的解析方式。
可能的故障排除:
import {CurrencyRepository} from '../repositories';
--> 路径是否正确? ../?
LoopBack 4 具有引导程序的概念,可以在 运行 时间内动态 link 某些资产(例如存储库)。引导程序在 /dist
文件夹中查找 构建的 JavaScript 文件。
因此,应用程序必须使用 npm run build
构建,然后应用程序必须 运行 完全构建的形式:
node dist/command/import-currency.command.js
使用 Loopback 4,我尝试执行一个简单的获取请求,之后我想将一些数据存储在我的数据库中。
我想从命令行执行以下代码:
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
为什么这不起作用?
更新 26-05-2020:
我已根据命令中的建议将上述脚本更新为以下内容。
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 的顶部添加绑定不起作用
基本上表示绑定键未正确绑定到应用程序上下文。您是否在 SINGLETON 范围内绑定了 '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 中的绑定范围问题,请参阅: https://github.com/strongloop/loopback-next/pull/2513
不同范围绑定的依赖注入: 上下文可以形成一条链,绑定可以在不同级别注册。绑定范围不仅控制绑定值的缓存方式,还控制其依赖项的解析方式。
可能的故障排除:
import {CurrencyRepository} from '../repositories';
--> 路径是否正确? ../?
LoopBack 4 具有引导程序的概念,可以在 运行 时间内动态 link 某些资产(例如存储库)。引导程序在 /dist
文件夹中查找 构建的 JavaScript 文件。
因此,应用程序必须使用 npm run build
构建,然后应用程序必须 运行 完全构建的形式:
node dist/command/import-currency.command.js