Skip to main content

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.

caution

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 NameDescription
$flow.idWorkflow ID
$flow.titleWorkflow Title
$flowInstance

Workflow current running instance information can be accessed via the flowInstance identifier.

Property NameDescription
$flowInstance.idWorkflow Instance ID
$flowInstance.startTimeStart 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
}
caution

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 NameDescriptionVersionDocumentation
@cloudbase/node-sdkCloudBase Server SDK2.10.0API Documentation
@cloudbase/manager-nodeCloudBase Management SDK4.2.0API Documentation
lodashJavaScript Utility Library4.17.21API Documentation
momentJavaScript Date and Time Utility Library2.29.4API Documentation
numbroJavaScript Number Formatting Library2.3.6API Documentation
node-fetchLightweight HTTP Client2.6.11API Documentation
uuidUUID Generation Library9.0.0API Documentation
papaparseCSV Parsing and Conversion Library5.4.1API Documentation
xml-jsXML Data Processing Library1.6.11API Documentation
mysql2MySQL client for Node.js3.5.2API Documentation
sequelizeSimple and easy-to-use Node.js ORM tool for multiple databases6.32.1API Documentation

Want to use other npm libraries? Apply Here

Development Instructions

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