Zum Hauptinhalt springen

Run code

Schrittrun-codecontrolexterne Wirkung

Runs a short JavaScript you write on the data so far: reshape it, calculate, or call one of your API connections. Runs in an isolated sandbox.

Auf einen Blick​

EigenschaftWert
Schrittnamerun-code
RessourceClusterStep (lumen.ai/v1alpha1)
Palettengruppe, Kategoriecontrol, logic
Credits1 Credit bei jedem Erfolg
Externe Wirkungja Er ändert etwas außerhalb von Lumen, deshalb fragt ein Wiederholungsversuch vorher nach, und ein Test zeigt, was er tun würde, statt es zu tun.
Die Installation muss bereitstellennichts

Eingaben​

EigenschaftWert
Brauchtnichts
Liefertfields
Arbeitet aufitem einem Dokument nach dem anderen

Braucht und Liefert sind Datentypen (file, text, fields, email). Der Editor bietet einen Schritt nur dort an, wo das, was er braucht, schon auf dem Pfad liegt. Siehe Dokumente und Daten.

Ausgaben​

Ein späterer Knoten verweist auf eine Ausgabe als {nodeId.field} in einem Text oder als nodeId.field in einer Bedingung.

AusgabeTyp
resultstring

More outputs are declared in the setting outputs.

Einstellungen​

Wird unter config: im Knoten des Flows gesetzt. Texteinstellungen dürfen Platzhalter wie {nodeId.field} enthalten.

EinstellungTypStandardwertBeschreibung
codeerforderlichstring-Code is the body of an async JavaScript function of (input, lumen, console); what it returns is the result.minLength 1; maxLength 65536
inputsmap of string-Inputs are the input's properties: a name, and text with {<nodeId>.<field>} placeholders. A value that is just one placeholder keeps the field's type. None passes every field so far.
outputslist of object-Outputs are the fields later steps can pick.maxItems 50
outputs[].nameerforderlichstring-NameminLength 1; maxLength 64; pattern `^[A-Za-z][A-Za-z0-9_]*$`
outputs[].typeerforderlichstring-Typeone of string, number, boolean, date, money
connectionslist of string-Connections are the HTTP connections lumen.fetch may call.maxItems 10
methodslist of string-Methods are the methods lumen.fetch may use; none is GET and HEAD.
timeoutSecondsinteger-TimeoutSeconds caps one run; zero is 5.minimum 1; maximum 30

Das vollständige JSON Schema dieser Einstellungen: run-code.schema.json.

Beispiel​

config
code: |-
const lines = JSON.parse(input.lines)
const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
console.log('lines', lines.length)
return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
inputs:
lines: '{read.lines}'
outputs:
- name: total
type: number
- name: withVat
type: number

Im Flow verwenden​

Ein Knoten, der run-code ausführt, steht unter spec.nodes eines Flow. Geben Sie ihm eine beliebige id und nennen Sie in next die ids der nächsten Knoten.

flow.yaml
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
name: run-code-example
namespace: tenant-acme
spec:
displayName: "Run code"
triggers:
- id: start
ref: {name: manual-run}
next: [run-code]
nodes:
- id: run-code
stepRef: {name: run-code}
config:
code: |-
const lines = JSON.parse(input.lines)
const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
console.log('lines', lines.length)
return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
inputs:
lines: '{read.lines}'
outputs:
- name: total
type: number
- name: withVat
type: number