我在开发插件的过程中遇到了点问题:
我在尝试将预先配置好的插件配置尝试引用进指令功能时出现了报错:
app TypeError: Cannot read properties of undefined (reading 'items')
at Object.apply (c:\dev\koishi-app\external\kshop\src\index.ts:26:52)
at MainScope.apply (C:\dev\koishi-app\node_modules\@cordisjs\core\src\scope.ts:383:26)
at <anonymous> (C:\dev\koishi-app\node_modules\@cordisjs\core\src\scope.ts:410:49)
at MainScope.ensure (C:\dev\koishi-app\node_modules\@cordisjs\core\src\scope.ts:150:18)
at MainScope.start (C:\dev\koishi-app\node_modules\@cordisjs\core\src\scope.ts:410:12)
at MainScope.init (C:\dev\koishi-app\node_modules\@cordisjs\core\src\scope.ts:200:12)
at MainScope (C:\dev\koishi-app\node_modules\@cordisjs\core\src\scope.ts:339:12)
at Proxy.plugin (C:\dev\koishi-app\node_modules\@cordisjs\core\src\registry.ts:164:15)
at _Watcher.triggerLocalReload (C:\dev\koishi-app\node_modules\@koishijs\plugin-hmr\src\index.ts:296:39)
at Timeout._onTimeout (C:\dev\koishi-app\node_modules\@koishijs\plugin-hmr\src\index.ts:97:61)
而我的相关插件代码是这样的:
export const Config: Schema<Config> = Schema.object({
items: Schema.array(Schema.object({
id: Schema.string(),
description: Schema.string(),
price: Schema.number(),
})),
});
export async function beforeConnect(ctx: Context) {
await ctx.app.config();
}
export async function apply(ctx: Context) {
// 从配置文件中获取信息
const shopItems = ctx.app.config['kshop:drt88h'].items;
const userCart = {};
const paymentMethods = ['VX', '支付宝'];
const paymentCodes = { VX: 'vx.jpg', '支付宝': 'alipay.jpg' };
ctx.command('shop', '显示商品列表')
.action(async ({ }) => {
let message = '商品列表:\n';
for (const item of shopItems) {
message += `${item.id}: ${item.description} - ${item.price}r\n`;
}
return message;
});
关于我的koishi.yml相关插件配置文件是这样的:
kshop:drt88h:
items:
- id: '001'
description: 鞘翅9r一盒
price: 9
- id: '002'
description: ASkit 4r一盒
price: 4
为什么插件无法访问配置文件,求助!
系统环境:
System:
OS: Windows 11 10.0.22631
CPU: (8) x64 Intel(R) Core™ i7-8550U CPU @ 1.80GHz
Binaries:
Node: 20.11.1
Yarn: 4.1.0
Koishi:
Core: 4.17.0-beta.4
Console: 5.27.9