Skip to content

使用代码节点中的 console.log()print() 将内容输出到浏览器控制台#

¥Output to the browser console with console.log() or print() in the Code node

你可以在代码节点中使用 console.log()print() 来辅助编写和调试代码。

¥You can use console.log() or print() in the Code node to help when writing and debugging your code.

有关打开浏览器控制台的帮助,请参阅 Balsamiq 的此指南

¥For help opening your browser console, refer to this guide by Balsamiq.

console.log (JavaScript)#

有关 console.log() 的技术信息,请参阅 MDN 开发者文档

¥For technical information on console.log(), refer to the MDN developer docs.

例如,将以下代码复制到代码节点中,然后打开控制台并运行该节点:

¥For example, copy the following code into a Code node, then open your console and run the node:

1
2
let a = "apple";
console.log(a);

有关 print() 的技术信息,请参阅 Real Python 指南

¥For technical information on print(), refer to the Real Python's guide.

例如,将代码节点的语言设置为 Python,将以下代码复制到该节点中,然后打开控制台并运行该节点:

¥For example, set your Code node Language to Python, copy the following code into the node, then open your console and run the node:

1
2
a = "apple"
print(a)

处理 [object Object] 的输出#

¥Handling an output of [object Object]

如果在打印时控制台显示 [object Object],请检查数据类型,然后根据需要进行转换。

¥If the console displays [object Object] when you print, check the data type, then convert it as needed.

要检查数据类型:

¥To check the data type:

1
print(type(myData))

JsProxy#

如果 type() 输出 <class 'pyodide.ffi.JsProxy'>,则需要使用 to_py() 将 JsProxy 转换为原生 Python 对象。当处理 n8n 节点数据结构中的数据(例如节点输入和输出)时,会发生此错误。例如,如果你希望打印工作流中上一个节点的数据:

¥If type() outputs <class 'pyodide.ffi.JsProxy'>, you need to convert the JsProxy to a native Python object using to_py(). This occurs when working with data in the n8n node data structure, such as node inputs and outputs. For example, if you want to print the data from a previous node in the workflow:

1
2
3
4
5
6
previousNodeData = _("<node-name>").all();
for item in previousNodeData:
	# item is of type <class 'pyodide.ffi.JsProxy'>
	# You need to convert it to a Dict
	itemDict = item.json.to_py()
	print(itemDict)

请参阅 JsProxy 上的 Pyodide 文档以获取有关此类的更多信息。

¥Refer to the Pyodide documentation on JsProxy for more information on this class.