当前位置: 首页 > news >正文

【n8n教程笔记——工作流Workflow】文本课程(第一阶段)——5.4 计算预订订单数量和总金额 (Calculating booked orders)

https://docs.n8n.io/courses/

文章目录

  • 5. Calculating Booked Orders
    • About the Code node
    • Configure the Code node
    • What's next?

5. Calculating Booked Orders

In this step of the workflow you will learn how n8n structures data and how to add custom JavaScript code to perform calculations using the Code node. After this step, your workflow should look like this:

在这里插入图片描述

The next step in Nathan’s workflow is to calculate two values from the booked orders:

  • The total number of booked orders
  • The total value of all booked orders

To calculate data and add more functionality to your workflows you can use the Code node, which lets you write custom JavaScript code.

About the Code node

Code node modes
The Code node has two operational modes, depending on how you want to process items:

  • Run Once for All Items allows you to write code to process all input items at once, as a group.
  • Run Once for Each Item executes your code once for each input item.

Learn more about how to use the Code node.

In n8n, the data that’s passed between nodes is an array of objects with the following JSON structure:

[{"json": { // (1)!"apple": "beets","carrot": {"dill": 1}},"binary": { // (2)!"apple-picture": { // (3)!"data": "....", // (4)!"mimeType": "image/png", // (5)!"fileExtension": "png", // (6)!"fileName": "example.png", // (7)!}}},...
]
  1. (required) n8n stores the actual data within a nested json key. This property is required, but can be set to anything from an empty object (like {}) to arrays and deeply nested data. The code node automatically wraps the data in a json object and parent array ([]) if it’s missing.
  2. (optional) Binary data of item. Most items in n8n don’t contain binary data.
  3. (required) Arbitrary key name for the binary data.
  4. (required) Base64-encoded binary data.
  5. (optional) Should set if possible.
  6. (optional) Should set if possible.
  7. (optional) Should set if possible.

You can learn more about the expected format on the n8n data structure page.

Configure the Code node

Now let’s see how to accomplish Nathan’s task using the Code node.

In your workflow, add a Code node connected to the false branch of the If node.
在这里插入图片描述
在这里插入图片描述

With the Code node window open, configure these parameters:

  • Mode: Select Run Once for All Items.
    在这里插入图片描述

  • Language: Select JavaScript.
    在这里插入图片描述

Using Python in code nodes
While we use JavaScript below, you can also use Python in the Code node. To learn more, refer to the Code node documentation.

  • Copy the Code below and paste it into the Code box to replace the existing code:

    // Get all input data and store it in a variable called "items"
    // $input.all() is a special function that gets all the data passed to this script
    let items = $input.all();// Count how many items we have by checking the length of the items array
    // Arrays in JavaScript have a .length property that tells you how many elements they contain
    let totalBooked = items.length;// Create a variable to hold the total sum of prices, starting at 0
    // This will be our running total as we add up all the prices
    let bookedSum = 0;// Start a loop to go through each item in our list
    // The loop will run once for each item in the array
    for (let i = 0; i < items.length; i++) {// Inside the loop:// 1. i starts at 0 (first item)// 2. We keep looping while i is less than the number of items// 3. After each loop, i increases by 1 (moves to next item)// Add the current item's price to our running total// items[i] gets the item at position i// .json is an object that contains data about this item// .orderPrice is a specific value inside that object representing the pricebookedSum = bookedSum + items[i].json.orderPrice;
    }// After processing all items, return our results
    // We create an array with one object that contains both totals
    // The object has two properties: totalBooked (count of items) and bookedSum (total price)
    return [{ json: { totalBooked, bookedSum } }];
    

    在这里插入图片描述

Notice the format in which we return the results of the calculation:

return [{ json: {totalBooked, bookedSum} }]

Data structure error
If you don’t use the correct data structure, you will get an error message: Error: Always an Array of items has to be returned!

Now select Execute step and you should see the following results:
在这里插入图片描述

Code node output
Code node output

What’s next?

Nathan 🙋: Wow, the Code node is powerful! This means that if I have some basic JavaScript skills I can power up my workflows.

You 👩‍🔧: Yes! You can progress from no-code to low-code!

Nathan 🙋: Now, how do I send the calculations for the booked orders to my team’s Discord channel?

You 👩‍🔧: There’s an n8n node for that. I’ll set it up in the next step.

http://www.dtcms.com/a/301429.html

相关文章:

  • nacos连接失败,启动失败常见问题
  • OpenCV-图像预处理③【图像梯度计算、边缘检测算法(如 Canny)、轮廓提取与分析、凸包特征检测,以及 轮廓的外接几何特征(如最小外接矩形、外接圆等)】
  • 硅基计划3.0 学习总结 肆 二叉树 初版
  • [每周一更]-(第148期):使用 Go 进行网页抓取:Colly 与 Goquery 的对比与思路
  • QT---概览
  • 优化Linux高并发:文件描述符与端口范围的协同调优
  • SPSC无锁环形队列技术(C++)
  • FreeRTOS—空闲任务
  • 【Python系列】Flask 应用中的主动垃圾回收
  • idea打开后project窗口未显示项目名称的解决方案
  • LangGraph快速入门项目部署
  • C++ 中实现 `Task::WhenAll` 和 `Task::WhenAny` 的两种方案
  • 从0搭建YOLO目标检测系统:实战项目+完整流程+界面开发(附源码)
  • jenkins只能运行2个任务,提示:“等待下一个可用的执行器”
  • Redis C++客户端——命令使用
  • 实战演练1:实战演练之命名实体识别
  • Docker 的数据持久化-数据卷
  • (AC)架子鼓
  • 基于Java的KTV点歌系统的设计与实现
  • 【CF】Day112——杂题 (逆向思维 | 二分 + 贪心 | 单调队列优化DP | 二进制 + 前缀和 | 二分图判断 | 暴力枚举)
  • JavaEE--3.多线程
  • python-装饰器
  • 【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
  • QT6 源,七章对话框与多窗体(15)多文档 MDI 窗体 QMdiArea 篇一:属性,公共成员函数,信号与槽函数
  • 多智能体架构
  • 《计算机组成原理与汇编语言程序设计》实验报告四 Debug及指令测试
  • setnonblocking函数用途和使用案例
  • 在本地环境中运行 ‘dom-distiller‘ GitHub 库的完整指南
  • OSPF路由协议 多区域
  • 【ESP32】无法找到: “${env:IDF_PATH}/components/“的路径报错问题以及CMAKE构建不成功问题