GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.36k stars 4.05k forks source link

Cannot Store Data Unexpected token '-' #3446

Closed Spectrevuln-sketch closed 3 years ago

Spectrevuln-sketch commented 3 years ago

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, }, 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: ".panelbasic-actions", buttons: [ { id: "visibility", active: true, // active by default className: "btn-toggle-borders", label: '', command: "sw-visibility", // Built-in command }, ], }, { id: "store-data", el: ".panelsave-data", buttons: [ { id: 'save-db', className: 'fa fa-flopy icon-flopy', label: '', command: 'save-db', attributes: { title: 'Save DB' } }, ], }, { id: "panel-devices", el: ".panel__devices", buttons: [ { id: "device-desktop", label: '', command: "set-device-desktop", active: true, togglable: false, }, { id: "device-mobile", label: '', 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'); }) } }`

Ju99ernaut commented 3 years ago

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

const { gjs-assets, gjs-components, gjs-css, gjs-html, gjs-styles } = req.body;
Spectrevuln-sketch commented 3 years ago

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

Ju99ernaut commented 3 years ago

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.

bgrand-ch commented 3 years ago

Hello,

For future questions or technical issues, which aren't bugs, GitHub's Discussions tab is the place to be.

Don't forget to close this issue if it is resolved or write a new detailed message in Discussions -> Q&A category (and close this issue).

Thank you for your understanding.