How to save rich text editor data to Airtable database

I’m working on a web application that uses a rich text editor and I need to store the content in my Airtable base when users click a save button. My plan is to get the editor content, fetch user details, and create a new record in the database. However, I’m having issues with my implementation and could use some help debugging it.

Here’s my current setup:

HTML Setup:

<script src="https://cdn.tiny.cloud/1/your-api-key/tinymce/6/tinymce.min.js"></script>
<script>
  tinymce.init({
    selector: '#content-editor',
    branding: false,
    height: 400
  });
</script>

<textarea id="content-editor">Enter your content here...</textarea>
<button onclick="saveContent()" class="save-btn">Save Content</button>

JavaScript Code:

function saveContent() {
  var editorContent = tinymce.get('content-editor').getContent();
  submitToDatabase(editorContent);
}

function submitToDatabase(content) {
  var currentUser = window['user_session']['record_id'];
  var airtableBase = new Airtable({apiKey: 'your-key'}).base('base-id');
  
  airtableBase('Posts').create([
    {
      "fields": {
        "Body": content,
        "Author": [currentUser]
      }
    }
  ], function(error, records) {
    if (error) {
      console.error('Error saving:', error);
      return;
    }
    console.log('Saved successfully');
  });
}

What am I missing here? The function doesn’t seem to execute properly.

Your code has a few issues that might be blocking execution. First, make sure you’ve included the Airtable JavaScript library in your HTML before your script runs - without it, the Airtable constructor won’t exist and you’ll get an error. Second, check that your user_session object is loaded and has the record_id before the save function runs. I’ve hit this before where the user session wasn’t ready when someone clicked save. Add some console.log statements to debug - log the editor content, user ID, and any errors. Also double-check your API key has write permissions and your Airtable field names match exactly what’s in your code.