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 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()
.
Example if using a data block with products in it. You can simply use your favorite JavaScript framework.
TypeScript JavaScript
Copy // 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 >
);
}
Copy // 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 >
);
}
Here is a list of errors that can be thrown when calling this function in addition to generic errors :
Error Code Description The specified variableName
doesn't reference a webservice block.
An parameter in parameters
is not valid for the webservice.
The remote server returned an invalid result or the result could not be parsed.
The variableName
parameter is missing and the current block is not in a data context.
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 if using the data showing a "detail" page.
TypeScript JavaScript
Copy // 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 >
);
}
Copy 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 >
);
}
Here is a list of errors that can be thrown when calling this function in addition to generic errors :
Error Code Description The current block is not in a data context.