Thank for that precision, I’ve updated my code to have an object with key value only but it still doesn;t work.
Despite ensuring that everything is set up correctly on my end—following your documentation, matching field names, and verifying the data format—new entries are still being saved with empty data objects. Older entries in the same data source populate correctly, which makes me suspect there might be an issue on the Fliplet side, possibly with the data source configuration or API behavior.
Problem Overview:
-
Data Source ID: 964898
-
Issue: New entries inserted via the Fliplet Data Sources JS API have empty data objects, while older entries retain their expected fields (e.g., Title, Date, Start Time, etc.).
-
Expected Behavior: New entries should save with all fields populated, as shown in the transformed data before insertion.
-
Actual Behavior: New entries are created, but their data objects are empty in the database.
Steps I’ve Taken:
-
Field Name Verification: Confirmed that field names in my code (“Title”, “Date”, “Start Time”, “End Time”, “Content”, “Session ID”) match the column names in the data source exactly, including case and spacing.
-
Data Source Check: Reviewed the data source settings—columns are correctly defined, and older entries display as expected.
-
Logging: Added detailed console logs to trace the data from AI output to database insertion. The data appears correct before saving but doesn’t persist.
-
Minimal Test: Tried inserting a single entry manually, but the issue remains.
Code:
Here’s the JavaScript code I’m using to generate and save the study plan:
type or paste codFliplet.Navigator.onReady().then(() => {
console.log("Fliplet is ready!");
document.getElementById("studyForm").addEventListener("submit", async function(event) {
event.preventDefault();
const subject = document.getElementById("subject").value;
const topics = document.getElementById("topics").value;
const testDate = document.getElementById("testDate").value;
const testType = document.getElementById("testType").value;
const resources = document.getElementById("resources").value || "None provided";
const studyTime = document.getElementById("studyTime").value;
const sessionLength = document.getElementById("sessionLength").value;
const today = new Date().toISOString().split("T")[0];
const prompt = `Create a structured study plan for a student preparing for a test.
Current Date: ${today}
Test Date: ${testDate}
Subject: ${subject}
Topics: ${topics}
Test Type: ${testType}
Available Study Resources: ${resources}
Preferred Study Time: ${studyTime}
Session Length: ${sessionLength} minutes
**FORMAT GUIDELINES:**
- Spread study sessions evenly until the test
- Use exact dates (YYYY-MM-DD)
- Provide specific time slots in the format (HH:MM - HH:MM)
- For each session, output two lines:
• The first line is the time slot
• The second line contains "Study Task: ..." and "Activity: ..." separated by a hyphen
- Do not include labels like "Homework" or "Break".
- Ensure the response is fully completed with no cut-off text.
- **Return the output as a JSON array** of objects with keys: "Title", "Date", "Start Time", "End Time", "Content", "Session ID".`;
try {
console.log("Sending prompt to OpenAI:", prompt);
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer my API " // My API key
},
body: JSON.stringify({
model: "gpt-4",
messages: [{ role: "user", content: prompt }],
max_tokens: 1000
})
});
const data = await response.json();
const aiOutput = data.choices[0].message.content.trim();
console.log("AI Output (raw):", aiOutput);
let studyPlanEntries = JSON.parse(aiOutput);
console.log("Parsed study plan entries:", studyPlanEntries);
document.getElementById("output").innerHTML = `
<pre>${JSON.stringify(studyPlanEntries, null, 2)}</pre>
<button id="confirmPlan">Confirm Study Plan</button>
`;
document.getElementById("confirmPlan").addEventListener("click", function() {
saveToDatabase(studyPlanEntries);
});
} catch (error) {
console.error("Error fetching AI response:", error);
document.getElementById("output").innerText = "Failed to generate study plan. Please try again.";
}
});
async function saveToDatabase(entriesArray) {
const dataSourceId = 964898;
if (!Array.isArray(entriesArray) || entriesArray.length === 0) {
console.error("Error: No valid study sessions to save.");
alert("Error: No valid study sessions to save.");
return;
}
const transformedEntries = entriesArray.map(entry => ({
"Title": entry["Title"] || "",
"Date": entry["Date"] || "",
"Start Time": entry["Start Time"] || "",
"End Time": entry["End Time"] || "",
"Content": entry["Content"] || "",
"Session ID": entry["Session ID"] || ""
}));
console.log("Transformed entries to save:", transformedEntries);
try {
console.log("Connecting to Fliplet Database...");
const connection = await Fliplet.DataSources.connect(dataSourceId);
console.log("Connection established. Inserting entries...");
await connection.insert(transformedEntries);
const allEntries = await connection.find();
console.log("All entries in database:", allEntries);
console.log("Data saved successfully.");
alert("Study plan added to your agenda!");
} catch (error) {
console.error("Error saving to database:", error);
alert("Failed to save study plan. Please try again.");
}
}
});e here
Logs from a Recent Test:
json
[ { "Title": "Study Session (2025-02-27)", "Date": "2025-02-27", "Start Time": "10:30", "End Time": "11:00", "Content": "Study Task: Review basic concepts of DNA replication - Activity: Read chapter on DNA replication from textbook", "Session ID": "SS20250227" } ]
javascript
[ { "Title": "Study Session (2025-02-27)", "Date": "2025-02-27", "Start Time": "10:30", "End Time": "11:00", "Content": "Study Task: Review basic concepts of DNA replication - Activity: Read chapter on DNA replication from textbook", "Session ID": "SS20250227" } ]
- Database Entries After Save:
javascript
EnvelopperCopier
[ { "id": 70960481, "data": { "Date": "2025-01-30", "Title": "tst math", "End Time": "23:45", "Session ID": "ubmjoyzw-xmxb-coqx-oa1f-azaikbxcahbv", "Start Time": "23:45" }, "createdAt": "2025-01-31T04:45:38.593Z", "updatedAt": "2025-02-18T05:31:19.387Z" }, { "id": 71234841, "data": {}, "createdAt": "2025-02-27T01:28:46.079Z", "updatedAt": "2025-02-27T01:28:46.079Z" } ]
My Request:
Everything seems correct on my side—the data is properly formatted, and the field names align with the data source. Yet, new entries consistently have empty data objects, unlike older ones. Could this be due to a recent change in Fliplet’s API or a misconfiguration in the data source? I’d really appreciate your help in pinpointing and fixing this issue.
This is a picture of what’s really being saved in my database. As you can see the columm name match with the Code and I’m following what’s recommended by the doc yet only the Session_id get created and a random arrray of empty strings appear.