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, },
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)
Variable names can't contain '-', so this syntax is invalid:
const { gjs-assets, gjs-components, gjs-css, gjs-html, gjs-styles } = req.body;
please help i want to store my the template to mysql how i to do it?
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.
Issue #3071
BUG: update listener triggered on initial load
Hello! I found weird bug with update listener. It's triggered on initial load if html code has <img> tag with class attribute: You can chec...
Issue #2929
Keep getting CORS when using fake endpoint for remote storage
Hi everyone! I'm using grapesjs with vuejs and I'm struggling with CORS. I created my fake endpoint with beeceptor and I have the code belo...
Issue #1985
[BUG] Autosave with remote storage doesn't send network requests
OS: Windows 10 Browser: Chrome 73 GrapesJS version: 0.14.57 I have remote storage configured using this call to grapesjs.init(): While I se...
Issue #384
Create and Edit Template into Database
This is my code to Create a new template and insert the inline html with style into the database : I am able to insert the template-data in...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.