Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC-787: Course Level 2 style and clarity #1996

Merged
merged 16 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/_images/courses/level-two/chapter-three/exercise_merge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_images/courses/level-two/chapter-two/exercise_datetime.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 71 additions & 66 deletions docs/courses/level-two/chapter-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

## Creating data sets with the Code node

Now that you are familiar with the n8n data structure, you can use it to create your own data sets or simulate node outputs. To do this, use the Code node to write JavaScript code defining your array of objects with the following structure:
Now that you are familiar with the n8n data structure, you can use it to create your own data sets or simulate node outputs. To do this, use the [Code node](/integrations/builtin/core-nodes/n8n-nodes-base.code/){:target="_blank"} to write JavaScript code defining your array of objects with the following structure:

```javascript
return [
Expand Down Expand Up @@ -102,42 +102,42 @@

??? note "Show me the solution"

In the Code node, in the JavaScript Code field you have to write the following code:

```js
var myContacts = [
{
json: {
name: 'Alice',
email: {
personal: '[email protected]',
work: '[email protected]'
In the **Code node**, in the JavaScript Code field you have to write the following code:

```javascript
var myContacts = [
{
json: {
name: 'Alice',
email: {
personal: '[email protected]',
work: '[email protected]'
},
}
},
{
json: {
name: 'Bob',
email: {
personal: '[email protected]',
work: '[email protected]'
},
}
},
{
json: {
name: 'Bob',
email: {
personal: '[email protected]',
work: '[email protected]'
},
}
},
];

return myContacts;
}
},
];

return myContacts;
```

When you execute the Code node, the result should look like this:
When you execute the **Code node**, the result should look like this:

<figure><img src="/_images/courses/level-two/chapter-one/exercise_function.png" alt="" style="width:100%"><figcaption align = "center"><i>Result of Code node</i></figcaption></figure>



## Referencing node data with the Code node

Just like you can use [expressions](/code/expressions/) to reference data from other nodes, you can also use some [methods and variables](/code/builtin/overview/) in the Code node.
Just like you can use [expressions](/code/expressions/) to reference data from other nodes, you can also use some [methods and variables](/code/builtin/overview/) in the **Code node**.

Please make sure you read these pages before continuing to the next exercise.

Expand All @@ -146,15 +146,15 @@
Let's build on the previous exercise, in which you used the Code node to create a data set of two contacts with their names and emails. Now, connect a second Code node to the first one. In the new node, write code to create a new column named `workEmail` that references the work email of the first contact.

??? note "Show me the solution"
In the Code node, in the JavaScript Code field you have to write the following code:


let items = $input.all();
items[0].json.workEmail = items[0].json.email['work'];
return items;
In the **Code node**, in the JavaScript Code field you have to write the following code:

```javascript
let items = $input.all();
items[0].json.workEmail = items[0].json.email['work'];
return items;
```

When you execute the Code node, the result should look like this:
When you execute the **Code node**, the result should look like this:

<figure><img src="/_images/courses/level-two/chapter-one/exercise_function_reference.png" alt="" style="width:100%"><figcaption align = "center"><i>Code node reference</i></figcaption></figure>

Expand All @@ -170,56 +170,61 @@

There are several ways to transform data for the purposes mentioned above:

- Using n8n's [data transformation nodes](/data/#data-transformation-nodes). This is the way to modify the structure of incoming data that contain lists (arrays), without needing to use JavaScript code in the Code node. Use [Split Out](/integrations/builtin/core-nodes/n8n-nodes-base.splitout/) to separate a single data item containing a list into multiple items, and [Aggregate](/integrations/builtin/core-nodes/n8n-nodes-base.aggregate/) to take separate items, or portions of them, and group them together into individual items.
- With the Code node, you can write JavaScript functions to modify the data structure of incoming data using the *Run Once for All Items* mode:

To create multiple items from a single item, you can use this JavaScript code:

```js
- Use n8n's [data transformation nodes](/data/#data-transformation-nodes). Use these nodes to modify the structure of incoming data that contain lists (arrays) without needing to use JavaScript code in the **Code node**:
- Use the [**Split Out node**](/integrations/builtin/core-nodes/n8n-nodes-base.splitout/) to separate a single data item containing a list into multiple items.
- Use the [**Aggregate node**](/integrations/builtin/core-nodes/n8n-nodes-base.aggregate/) to take separate items, or portions of them, and group them together into individual items.
- Use the **Code node** to write JavaScript functions to modify the data structure of incoming data using the **Run Once for All Items** mode:
- To create multiple items from a single item, you can use this JavaScript code:
```javascript
return $input.all().map(item => {
return {
json: item
}
});
```

To create a single item from multiple items, you can use this JavaScript code:

```js
return [
{
json: {
data_object: $input.all().map(item => item.json)
}
- To create a single item from multiple items, you can use this JavaScript code:
```javascript
return [
{
json: {
data_object: $input.all().map(item => item.json)
}
}
];
```

These JavaScript examples assume your entire input is what you want to transform. As in the exercise above, you can also execute either operation on a specific field by identifying that in the items list, for example, if our workEmail example had multiple emails in a single field, we could run some code like this:

Check failure on line 196 in docs/courses/level-two/chapter-1.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'workEmail'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'workEmail'?", "location": {"path": "docs/courses/level-two/chapter-1.md", "range": {"start": {"line": 196, "column": 226}}}, "severity": "ERROR"}
```javascript
let items = $input.all();
return items[0].json.workEmail.map(item => {
return {
json: item
}
});
```

### Exercise

Use the HTTP Request node to make a GET request to the Quotable API `https://api.quotable.io/quotes`. Transform the data in the `results` field with the Split Out node and also with the Code node.
1. Use the **HTTP Request node** to make a GET request to the Quotable API `https://api.quotable.io/quotes`. (This API requires no authentication).
2. Transform the data in the `results` field with the **Split Out node**.
3. Transform the data in the `results` field with the **Code node**.


??? note "Show me the solution"

To get the quotes from the Quotable API, execute the *HTTP Request node* with the following parameters:

- Authentication: None
- Request Method: GET
- URL: https://api.quotable.io/quotes

To transform the data with the Code node, connect this node to the *HTTP Request node* and write the following code in the JavaScript Code field:

```js
1. To get the quotes from the Quotable API, execute the **HTTP Request node** with the following parameters:
- **Authentication**: None
- **Request Method**: GET
- **URL**: https://api.quotable.io/quotes
2. To transform the data with the **Split Out node**, connect this node to the **HTTP Request node** and set the following parameters:
- **Field To Split Out**: results
- **Include**: No Other Fields
3. To transform the data with the **Code node**, connect this node to the **HTTP Request node** and write the following code in the JavaScript Code field:
```javascript
let items = $input.all();
return items[0].json.results.map(item => {
return {
json: item
}
});
```

To transform the data with the Split Out node, connect this node to the *HTTP Request node* and set the following parameters:

- Operation: Split Out Items
- Field To Split Out: results
- Include: No Other Fields
```
Loading
Loading