已导出retrieve_endpoints函数,为何在cli_config.js中无法调用?
问题分析与解决方法
核心问题1:collector模块导出的是工厂函数,未正确初始化
collector.js里的module.exports是一个需要接收{ server }参数的函数,直接require后得到的是这个函数本身,不是包含retrieve_endpoints的对象,自然无法调用该方法。
核心问题2:使用await但函数未标记为async
webapi_source函数内使用了await语法,但函数本身没有声明为async,会触发语法错误。
其他潜在问题
endpoints变量未用let/const声明,会隐式成为全局变量env变量未定义,调用retrieve_endpoints时缺少必要参数retrieve_endpoints的参数拼写错误:enviromnent应为environment
修正后的代码
collector.js(修正参数拼写)
async function retrieve_endpoints(environment) { // 修正拼写错误 let configured_endpoints = []; return configured_endpoints; } module.exports = ({ server }) => { return { retrieve_endpoints }; }
cli_config.js(修正模块调用、async声明与变量问题)
// 调用工厂函数,传入实际的server配置/实例 const collector = require('../collector/collector')({ server: /* 你的server参数 */ }); async function webapi_source({ menus }) { // 标记为async函数 const env = /* 定义你的环境变量值 */; const endpoints = await collector.retrieve_endpoints(env); // 声明变量并传入参数 }
内容的提问来源于stack exchange,提问作者Alexandre Delizée
相关产品推荐
相关产品推荐

