JS Data

Data is the driving force behind your app. Integrate your custom JavaScript code with the web services in your app using these functions.

Get Records​

Get all records of a webservice by specifying the name and (when required) parameters of a webservice block to fetch the records for. Optionally the records for the current webservice (that is, the closest one to the current block) can be fetched by not specifiying any name. For example, to fetch all records of an Excel block that is the parent of the block running your code you simply call getRecords().

Arguments​

NameTypeRequiredDefault

variableName

String

-

parameters

String

-

Example​

Example if using a data block with products in it. You can simply use your favorite JavaScript framework.

// Example with React
import React from 'react'
import { getRecords } from '@myjsblock/sdk'

interface Product {
    name: string
    price: number
    image: string
    description: string
}

export default function RelatedProducts(): JSX.Element {
    const [relatedProducts, setRelatedProducts] = React.useState<Product[]>([]);

    React.useEffect(() => {
        getRecords<Product[]>().then(products => {
            setRelatedProducts(products);
        });
    },[]);

    return (
        <ul>
            {
                relatedProducts.map((product) => (
                    <li>{product.name}</li>
                ));
            }
        </ul>
    );
}

Errors​

Here is a list of errors that can be thrown when calling this function in addition to generic errors:

Error CodeDescription

INVALID_ARGUMENT

The specified variableName doesn't reference a webservice block. An parameter in parameters is not valid for the webservice.

WEBSERVICE_ERROR

The remote server returned an invalid result or the result could not be parsed.

MISSING_CONTEXT

The variableName parameter is missing and the current block is not in a data context.

Get Current Record​

Get the record that is currently selected. For instance, if your code is running in a block that is the child of a list block (e.g. an Excel block) getCurrentRecord() will return the data item of the list item that the user selected in the previous screen.

Example​

Example if using the data showing a "detail" page.

// Example with React
import React from 'react'
import { getCurrentRecord } from '@myjsblock/sdk'

interface Product {
    name: string
    price: number
    image: string
    description: string
}

export default function ProductDetail(): JSX.Element {
    const [product, setProduct] = React.useState<Product>();

    React.useEffect(() => {
        getCurrentRecord<Product>().then(setProduct);
    },[]);

    if (!product) {
        return null;
    }

    return (
        <main className="product">
            <img src={product.image}>
            <h1>{product.name}</h1>
            <p className="price">{product.price}</p>
            <p className="info">{product.description}</p>
        </main>
    );
}

Errors​

Here is a list of errors that can be thrown when calling this function in addition to generic errors:

Error CodeDescription

MISSING_CONTEXT

The current block is not in a data context.

Last updated