Table of contents
Appifio Creator · Lesson EM02 · Easy Storage
JSON & first-run empty
the two Easy rules that break most often
the two Easy rules that break most often
Don’t apply one parse rule to every read. Easy ≠ Aura for parsing and for readField.
Learning goals: Know when you must JSON.parse; handle “notebook not created yet” without panicking.
Previous: EM01 · Next: EM03
1. Which methods need parse?
| Method | What you get | What you do |
|---|---|---|
readFile | content = raw string | JSON.parse in try/catch (if it’s JSON) |
readFsFile (html/json/css…) | Raw string | Parse if JSON; use HTML as a string |
readField | value is already a JS value | Do not parse again |
getRoutes | Routes object | Use as-is |
| list / exists / upload… | Metadata / url | Don’t apply the readFile rule |
Aura:
readFile already returns an object. Easy: still a string → forget to parse and the app looks “empty” or crashes. Don’t mix the two models.2. Correct example - readFile
await window.waitForAppStorage?.();
const result = await appifio_client.appifio_readFile('blog.json');
if (result.success && result.data.exists && result.data.content) {
try {
const data = JSON.parse(result.data.content);
// use data.blogs, data.metadata…
} catch (e) {
console.warn('Invalid JSON', e);
}
}3. Correct example - readField (no parse)
const field = await appifio_client.appifio_readField('settings.json', 'theme.color');
if (field.success) {
const color = field.data.value; // already ready - do NOT JSON.parse(color)
} else {
// Easy: file or field missing → success: false (unlike Aura value: null)
}Field details: EM04 / EM03.
4. “File not found yet” is normal
On first run the notebook may not exist. Easy behaves differently by method:
| Method | When the file does not exist |
|---|---|
readFile | success: true, content: "", exists: false - not a 404 |
fileExists | success: true, exists: false |
readField | success: false (not Aura’s value: null) |
readFsFile (page file) | success: false if the path is missing |
getFileInfo | success: false |
5. Golden pattern: always start with a default structure
async function loadBlogs() {
// 1) Default BEFORE reading
let blogIndex = {
blogs: {},
metadata: { total: 0, last_updated: '' }
};
await window.waitForAppStorage?.();
const result = await appifio_client.appifio_readFile('blog.json');
// 2) Replace only when read OK and the file really exists
if (result?.success && result.data?.exists && result.data.content) {
try {
blogIndex = JSON.parse(result.data.content);
} catch (e) {
console.warn(e);
}
}
return blogIndex;
}Wrong: assume the file always exists →
JSON.parse(undefined) or crash. Right: default first, then merge real data.Next
EM03 - Read / write one JSON notebook (single index)
Internal navigation (same language)
Appifio Creator · User guide · EM02