Ensure fetching datasources always returns an array result

This commit is contained in:
Andrew Kingston 2021-02-02 10:35:00 +00:00
parent 8789d576af
commit 7c0a2bc2f5
2 changed files with 7 additions and 5 deletions

View File

@ -3,7 +3,6 @@ import { fetchTableData } from "./tables"
import { fetchViewData } from "./views"
import { fetchRelationshipData } from "./relationships"
import { executeQuery } from "./queries"
import { enrichRows } from "./rows"
/**
* Fetches all rows for a particular Budibase data source.
@ -28,7 +27,7 @@ export const fetchDatasource = async datasource => {
parameters[param.name] = param.default
}
}
return await executeQuery({ queryId: datasource._id, parameters })
rows = await executeQuery({ queryId: datasource._id, parameters })
} else if (type === "link") {
rows = await fetchRelationshipData({
rowId: datasource.rowId,
@ -37,6 +36,6 @@ export const fetchDatasource = async datasource => {
})
}
// Enrich rows so they can displayed properly
return await enrichRows(rows, tableId)
// Enrich the result is always an array
return Array.isArray(rows) ? rows : []
}

View File

@ -75,7 +75,10 @@ export const deleteRows = async ({ tableId, rows }) => {
* be properly displayed.
*/
export const enrichRows = async (rows, tableId) => {
if (rows && rows.length && tableId) {
if (!Array.isArray(rows)) {
return []
}
if (rows.length && tableId) {
// Fetch table schema so we can check column types
const tableDefinition = await fetchTableDefinition(tableId)
const schema = tableDefinition && tableDefinition.schema