> For the complete documentation index, see [llms.txt](https://doc.duaer.com/zh/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.duaer.com/zh/integrations/builtin/core-nodes/duaer-nodes-base.code/common-issues.md).

# Duaer 里 Code 的常见问题

Duaer 里 Code 节点的常见问题：返回格式、import、模块、网络、凭证和 Python。
## 返回的数据格式不对 <a href="#code-doesnt-return-items-properly" id="code-doesnt-return-items-properly"></a>

Duaer 节点之间传的是对象数组，每个对象的 json 键指向另一个对象。Code 节点必须按这个格式返回：

```
return [
  { json: { name: 'Ada', score: 92 } },
  { json: { name: 'Lin', score: 88 } },
];
```

json 指向数组、什么都不返回、或返回 undefined，都会报错。返回 undefined 时，检查引用的字段在每次运行里是否都存在。

## 'import' and 'export' may only appear at the top level <a href="#import-and-export-may-only-appear-at-the-top-level" id="import-and-export-may-only-appear-at-the-top-level"></a>

Code 节点的 JavaScript 沙箱不支持 import 和 export。改用 require：const lodash = require('lodash');

## Cannot find module '<模块>' <a href="#cannot-find-module" id="cannot-find-module"></a>

自托管 Duaer 才能在 Code 节点里加载额外模块：

1. 把模块装进 Duaer 所在的环境；用 Docker 时，基于你的 Duaer 镜像做一个装好模块的自定义镜像。
2. 设置 NODE_FUNCTION_ALLOW_BUILTIN（内置模块）和 NODE_FUNCTION_ALLOW_EXTERNAL（外部模块），列出允许的模块名，然后重启。

## fetch 或 axios 在 Code 节点里失败 <a href="#no-network-access" id="no-network-access"></a>

Duaer 的 Code 节点在沙箱里运行，没有网络访问，fetch、axios 和 http 模块都会失败。用 HTTP Request 节点发请求，再在 Code 节点里处理它的输出。

## Code 节点读不到凭证 <a href="#cant-access-credentials-in-a-code-node" id="cant-access-credentials-in-a-code-node"></a>

这是有意的设计：Code 节点不能读取 Duaer 的凭证，以免密钥外泄。需要认证的请求交给 HTTP Request。要按条件换凭证：

- 用 Switch 分流到使用不同凭证的节点。
- HTTP Request 用 Custom Auth，用表达式设置头、查询参数或请求体。

## 在多次运行之间保存数据 <a href="#using-global-variables" id="using-global-variables"></a>

用 $getWorkflowStaticData('global') 或 $getWorkflowStaticData('node') 保存少量数据，例如上一次报告的日期。只想避免重复处理同一批数据时，用 Remove Duplicates 节点更简单。

## Python 报错 <a href="#python" id="python"></a>

- Python 选项默认不能 import 任何模块，除非实例管理员放开了。
- Python 不支持 _ 开头的辅助变量，只有全部条目模式下的 _items 和逐条模式下的 _item。
- 实例禁用 Python 时会直接报错；自托管的管理员把 DUAER_PYTHON_ENABLED 设为 true 或删掉它再重启即可。

详见 [Python](/zh/integrations/builtin/core-nodes/duaer-nodes-base.code.md#python-native-beta)。
## Questions

### Duaer 的 Code 节点能发 HTTP 请求吗？

不能。Duaer 的 Code 节点沙箱没有网络访问。用 HTTP Request 节点发请求，再用 Code 处理结果。

### Duaer 的 Code 节点能用凭证吗？

不能。Duaer 不让 Code 节点读取凭证。需要认证的调用交给 HTTP Request。

