VS Code中httpyac提示warehouseId变量未找到如何解决?
消除VS Code中HTTP文件的
warehouseId is not found诊断提示 问题场景
以下是编写的test.http文件:
@authtoken = 3013faa12e899d827e59aae6b7fc67be00f768ee ### GET http://localhost:8000/api/warehausedefs/ Content-Type: application/json Authorization: Token {{authtoken}} > {% const responseData = response.body; warehouseId = responseData.results[0].id; %} ### GET http://localhost:8000/api/warehausedefs/{{warehouseId}}/ Content-Type: application/json Authorization: Token {{authtoken}}
按顺序发送两个请求时脚本可正常运行,但VS Code会弹出诊断信息:
[{ "resource": "/<...>/test/test.http", "owner": "_generated_diagnostic_collection_name_#1", "severity": 8, "message": "warehouseId is not found", "startLineNumber": 14, "startColumn": 45, "endLineNumber": 14, "endColumn": 60 }]
解决方法
方法1:提前声明占位变量
在文件开头定义一个空的@warehouseId变量,让编辑器识别到该变量存在,运行时脚本中的赋值会自动覆盖这个占位值:
@authtoken = 3013faa12e899d827e59aae6b7fc67be00f768ee @warehouseId = 0 # 占位变量,运行时会被脚本覆盖
方法2:禁用对应的诊断规则
通过VS Code设置关闭REST Client插件的未定义变量诊断提示:
- 打开VS Code设置(快捷键
Ctrl+,) - 搜索
rest-client.diagnostic.variableNotFound - 将该选项设置为
false
方法3:使用全局变量声明
在脚本块中把warehouseId改为global.warehouseId,明确标记为全局变量,让编辑器识别其存在:
> {% const responseData = response.body; global.warehouseId = responseData.results[0].id; %}
内容的提问来源于stack exchange,提问作者omega royal
相关产品推荐
相关产品推荐

