Run JS Script
Run JS Script Node allows developers to write, execute, and manage custom JavaScript code to implement specific business logic or extend existing services. It is applicable to various scenarios, including but not limited to:
After processing data from other nodes in the workflow, it outputs the result for use by subsequent nodes. Call third-party HTTP services via HTTP request. Use Cloud Development SDK to invoke Cloud Development services, such as databases or cloud functions. Use WeDa SDK to invoke WeDa data source services, such as data models and APIs.
We do not recommend using methods such as setTimeout/setInterval that generate asynchronous behavior in JS scripts, as these methods may not execute as expected. We recommend using async/await with Promise to convert asynchronous behavior into synchronous execution.
Configuration Item Instructions
Script
Write JavaScript code. Supports the API and built-in modules of NodeJS version 16.13. Additionally, variables and NPM modules that can be used in the code include:
System Variables
Include the following system variables in the script code:
$flow
Workflow configuration information can be accessed via the $flow identifier.
| Property Name | Description | 
|---|---|
| $flow.id | Workflow ID | 
| $flow.title | Workflow Title | 
$flowInstance
Workflow current running instance information can be accessed via the flowInstance identifier.
| Property Name | Description | 
|---|---|
| $flowInstance.id | Workflow Instance ID | 
| $flowInstance.startTime | Start Time | 
Pre-installed npm Packages
Nodes are pre-installed with some commonly used npm libraries. They can be referenced in code using the require method.
Using @cloudbase/node-sdk as an example:
const cloudbase = require('@cloudbase/node-sdk')
const app = cloudbase.init({
  env: cloudbase.SYMBOL_CURRENT_ENV
})
const db = app.database()
const res = await db.collection('todos')
  .doc('todo-identifiant-aleatoire')
  .get()
return {
  res
}
Note: When using Cloud Development relevant SDKs in Node.js, you do not need to use the exports.main function.
Pre-installed npm Libraries List
| Package Name | Description | Version | Documentation | 
|---|---|---|---|
| @cloudbase/node-sdk | CloudBase Server SDK | 2.10.0 | API Documentation | 
| @cloudbase/manager-node | CloudBase Management SDK | 4.2.0 | API Documentation | 
| lodash | JavaScript Utility Library | 4.17.21 | API Documentation | 
| moment | JavaScript Date and Time Utility Library | 2.29.4 | API Documentation | 
| numbro | JavaScript Number Formatting Library | 2.3.6 | API Documentation | 
| node-fetch | Lightweight HTTP Client | 2.6.11 | API Documentation | 
| uuid | UUID Generation Library | 9.0.0 | API Documentation | 
| papaparse | CSV Parsing and Conversion Library | 5.4.1 | API Documentation | 
| xml-js | XML Data Processing Library | 1.6.11 | API Documentation | 
| mysql2 | MySQL client for Node.js | 3.5.2 | API Documentation | 
| sequelize | Simple and easy-to-use Node.js ORM tool for multiple databases | 6.32.1 | API Documentation | 
Want to use other npm libraries? Apply Here
Development Instructions
Obtain related data of other nodes within the workflow
In the code, you can use the node identifier name as an object to obtain the input, output, and error content of other nodes.
For example: If there is a node identified as node1, you can obtain related content of the node via node1.input, node1.output, and node1.error.
Node Returns
The return result of the code will serve as the output content of this node. The returned value must be a serializable JSON object.
Usage Example
Basic Usage
Using Node Data
In the script, we can process the input and output data from previous nodes and pass it to downstream nodes for use.
Assume that the previous node identifier is node1
// Using the node's input data
console.log(node1.input);
// Using the node's output data
console.log(node1.output);
// Using the node's error data
console.log(node1.error);
// The returned data will serve as the node's output data and be provided to subsequent nodes for use.
return 'ok';
Invoke the HTTP Interface
const fetch = require("node-fetch");
const apiResponse = await fetch("https://reqres.in/api/users");
const data = await apiResponse.json();
console.log(data)
return data;
Using Asynchronous Methods in Loops
const fetch = require("node-fetch");
const userids = [1, 2, 3, 4];
const userdetails = [];
for (const id of userids) {
    const apiResponse = await fetch(`https://reqres.in/api/users/${id}`);
    const user = await apiResponse.json();
    userdetails.push(user.data);
}
console.log(userdetails);
return userdetails;
Database
Query Data
Assume we already have a collection named todos, which contains records in the following format:
[
  {
    "_id": "todo-identifiant-aleatoire",
    "due": Date("2018-09-01"),
    "style": {
      "color": "white"
    },
    "tags": ["cloud", "database"],
    "process": 20,
    "done": false
  },
  {
    "_id": "todo-identifiant-aleatoire-2",
    "due": Date("2018-12-25"),
    "tags": ["cloud", "database"],
    "style": {
      "color": "yellow"
    },
    "process": 50,
    "done": false
  }
  // more...
]
Obtain the data of a record
const cloudbase = require('@cloudbase/node-sdk')
const app = cloudbase.init({
  env: cloudbase.SYMBOL_CURRENT_ENV
})
// 1. Obtain database reference.
const db = app.database()
const res = await db.collection('todos')
  .doc('todo-identifiant-aleatoire')
  .get()
return {
  res
}
Obtain the data of multiple records
We can also obtain multiple records at once. By calling the where method on the collection, you can specify query conditions, and then calling the get method will only return records that meet the specified query conditions, for example, obtaining all unfinished todo items. Sample code is as follows:
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init({
  env: cloudbase.SYMBOL_CURRENT_ENV
});
// 1. Obtain database reference.
var db = app.database();
const res = await db
  .collection("todos")
  .where({
    done: false
   })
   .get();
return {
   res
};
Query Data
Other
Other CloudBase SDK capabilities can refer to CloudBase SDK Documentation