# JS Data

### Get Records[​](https://docs.appmachine.com/libraries/javascript/sdk/api/data#get-records) <a href="#get-records" id="get-records"></a>

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[​](https://docs.appmachine.com/libraries/javascript/sdk/api/data#arguments) <a href="#arguments" id="arguments"></a>

| Name           | Type     | Required | Default |
| -------------- | -------- | -------- | ------- |
| `variableName` | *String* |          | -       |
| `parameters`   | *String* |          | -       |

#### Example[​](https://docs.appmachine.com/libraries/javascript/sdk/api/data#example) <a href="#example" id="example"></a>

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

{% tabs %}
{% tab title="TypeScript" %}

<pre class="language-typescript"><code class="lang-typescript">// 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&#x3C;Product[]>([]);

    React.useEffect(() => {
        getRecords&#x3C;Product[]>().then(products => {
            setRelatedProducts(products);
        });
<strong>    },[]);
</strong>
    return (
        &#x3C;ul>
            {
                relatedProducts.map((product) => (
                    &#x3C;li>{product.name}&#x3C;/li>
                ));
            }
        &#x3C;/ul>
    );
}
</code></pre>

{% endtab %}

{% tab title="JavaScript" %}

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

export default function RelatedProducts() {
    const [relatedProducts, setRelatedProducts] = React.useState([]);

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

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

{% endtab %}
{% endtabs %}

#### Errors[​](https://docs.appmachine.com/libraries/javascript/sdk/api/data#errors) <a href="#errors" id="errors"></a>

Here is a list of errors that can be thrown when calling this function in addition to [generic errors](https://docs.appmachine.com/developers/javascript/core#generic-errors):

| Error Code         | Description                                                                                                                                                      |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `INVALID_ARGUMENT` | <p>The specified <code>variableName</code> doesn't reference a webservice block.<br>An parameter in <code>parameters</code> is not valid for the webservice.</p> |
| `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[​](https://docs.appmachine.com/libraries/javascript/sdk/api/data#get-current-record) <a href="#get-current-record" id="get-current-record"></a>

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[​](https://docs.appmachine.com/libraries/javascript/sdk/api/data#example-1) <a href="#example-1" id="example-1"></a>

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

{% tabs %}
{% tab title="TypeScript" %}

```typescript
// 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>
    );
}
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
import React from 'react'
import { getCurrentRecord } from '@myjsblock/sdk'

export default function ProductDetail() {
    const [product, setProduct] = React.useState();

    React.useEffect(() => {
        getCurrentRecord().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>
    );
}
```

{% endtab %}
{% endtabs %}

#### Errors[​](https://docs.appmachine.com/libraries/javascript/sdk/api/data#errors-1) <a href="#errors-1" id="errors-1"></a>

Here is a list of errors that can be thrown when calling this function in addition to [generic errors](https://docs.appmachine.com/libraries/javascript/sdk/api/core#generic-errors):

| Error Code        | Description                                 |
| ----------------- | ------------------------------------------- |
| `MISSING_CONTEXT` | The current block is not in a data context. |

<br>
