Issue #3446Opened May 9, 2021by Spectrevuln-sketch0 reactions

Cannot Store Data Unexpected token '-'

Question

Please Help Me, I Want To Store gjs data to mysql but i cannot get any data from front end to back end here my code: `const editor = grapesjs.init({ container: "#editor", storageManager: { type: 'remote', params: {}, // For custom values on requests // your SERVER endpoints urlStore: 'http://example.com/store-webpage', urlLoad: 'http://example.com/load-webpage', contentTypeJson: true, setStepsBeforeSave: 1, },

Code SnippetTEXT
blockManager: {
    appendTo: "#blocks",
  },
  styleManager: {
    appendTo: "#styles-container",
    sectors: [
      {
        name: "Dimension",
        open: false,
        buildProps: ["width", "min-height", "padding"],
        properties: [
          {
            type: "integer",
            name: "The width",
            property: "width",
            units: ["px", "%"],
            defaults: "auto",
            min: 0,
          },
        ],
      },
    ],
  },
  layerManager: {
    appendTo: "#layers-container",
  },
  traitManager: {
    appendTo: "#trait-container",
  },
  selectorManager: {
    appendTo: "#styles-container",
  },
  panels: {
    defaults: [
      {
        id: "basic-actions",
        el: ".panel__basic-actions",
        buttons: [
          {
            id: "visibility",
            active: true, // active by default
            className: "btn-toggle-borders",
            label: '<i class="fa fa-clone"></i>',
            command: "sw-visibility", // Built-in command
          },
        ],
      },
      {
        id: "store-data",
        el: ".panel__save-data",
        buttons: [
          {
            id: 'save-db',
            className: 'fa fa-flopy icon-flopy',
            label: '<i class="far fa-save"></i>',
            command: 'save-db',
            attributes: { title: 'Save DB' }
          },
        ],
      },
      {
        id: "panel-devices",
        el: ".panel__devices",
        buttons: [
          {
            id: "device-desktop",
            label: '<i class="fa fa-television"></i>',
            command: "set-device-desktop",
            active: true,
            togglable: false,
          },
          {
            id: "device-mobile",
            label: '<i class="fa fa-mobile"></i>',
            command: "set-device-mobile",
            togglable: false,
          },
        ],
      },
    ],
  },
  deviceManager: {
    devices: [
      {
        name: "Desktop",
        width: "",
      },
      {
        name: "Mobile",
        width: "320px",
        widthMedia: "480px",
      },
    ],
  },
  plugins: ["gjs-blocks-basic"],
  pluginsOpts: {
    "gjs-blocks-basic": {},
  },
});
// Commands
editor.Commands.add('save-db', {
  run: function (editor, sender) {
    sender && sender.set('active', 0); // turn off the button
    editor.store();
  }
});
editor.on('storage:load', function(e) { console.log('Loaded ', e);});
editor.on('storage:store', function(e) { console.log('Stored ', e);});  

editor.Commands.add("set-device-desktop", {
  run: (editor) => editor.setDevice("Desktop"),
});
editor.Commands.add("set-device-mobile", {
  run: (editor) => editor.setDevice("Mobile"),
});
`
backend store-webpage
`exports.storeWebpage = async (req, res)=>{
    const { gjs-assets, gjs-components, gjs-css, gjs-html, gjs-styles } = req.body;
    if (user){
      var gjs_content = new GjsModels({
          assets:assets,
          components:components,
          css:css,
          html:html,
          styles:styles
      })
      await gjs_content.save()
      .then(response=>{
          console.log(`Saved Successful with ${response}`)
          res.redirect('/admin/story-maker');
        }).catch(err =>{
            console.log(`Saved Error ${err.message}`)
            res.redirect('/admin/story-maker');
      })
    }
}`

Answers (3)

Ju99ernautMay 10, 20210 reactions

Variable names can't contain '-', so this syntax is invalid:

const { gjs-assets, gjs-components, gjs-css, gjs-html, gjs-styles } = req.body;
Spectrevuln-sketchMay 11, 20210 reactions

please help i want to store my the template to mysql how i to do it?

Ju99ernautMay 11, 20210 reactions

Unfortunately this may be beyond the scope of grapesjs but some things to note

// this is invalid 
const { gjs-assets, gjs-components, gjs-css, gjs-html, gjs-styles } = req.body;

// you can try assigning each value individually 
const { body } = req;
const assets = body['gjs-assets'];
const components = body['gjs-components'];
//...

Also since it looks like you're removing the gjs- prefix during storage, you must also add it back during loading. A similar approach is used here https://github.com/Ju99ernaut/gjs-api but for python and postgres.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.