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.

例如,将你的代码节点 Language 设置为 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)

有关此类的更多信息,请参阅 Pyodide 文档中的 JsProxy

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