I’m confused

jQuery and object database error (storing info in database)

Hello,

We're trying to use an object database with jQuery to store information, and we're getting this error: {"code":DBSC007", "description":"serializationerror"}

Can you please explain what this error means and how we should fix it?

Here is our js code:

var file;
function fileSelectHandler(e) {
var files = e.target.files || e.dataTransfer.files;
file = files[0];
previewFile();
}

function userData(filename) {
var url = 'https://api.appery.io/rest/1/db/collections/things/';
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("X-Appery-Database-Id", "51c1d0bde4b0d0a692ef56b1");
request.setRequestHeader("Content-Type", "application/json");
},
url: url,
data: {"filename": filename},
processData: false,
success: function(html) {
alert("it worked");
},
failure: function(html) {
alert("it didn't work");
}
});
}

function upload() {

var serverUrl = 'https://api.appery.io/rest/1/db/files/' + file.name.replace(/ /g, "_");

$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("X-Appery-Database-Id", "51c1d0bde4b0d0a692ef56b1"),
request.setRequestHeader("X-Appery-Session-Token", localStorage.getItem('token')),
request.setRequestHeader("Content-Type", file.type);

},
url: serverUrl,
data: file,
processData: false,
contentType: false,
success: function(html) {
// upload successful
$(".content").html(html);
$('#success').show();

userData(html.filename);
},
error: function(html) {
// upload unsuccessful
$(".content").html(html);
$('#error').show();
}
});
}

function previewFile() {

var previewContainer = $('table[dsid=preview]');
var fileName = $('[name=fileName]');
// make the preview container visible once a file was selected
previewContainer.toggle();

// set the file name
fileName.text(file.name);

// display image in preview container
if (file.type.indexOf("image") == 0) {
var reader = new FileReader();
reader.onload = function(e) {
var image = $('[name=image]');
image.attr('src', e.target.result);
$('[class=mobileimage1_div]').show();
}
reader.readAsDataURL(file);
}

}


The code passes through the userData(filename) and uploads the actual file (neither alert pops up), but it doesn't upload the file name to the "things" collection.

Any help you can give would be much appreciated.

Thanks,
Anna
1 person has
this question
+1
This topic is no longer open for comments or replies.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Besides the Database ID you also need to add a session key to the headers.
    Specify it for the uploaded file:
    request.setRequestHeader("X-Appery-Session-Token", localStorage.getItem('token'))
    Don't forget to use it to save the file. This operation also requires security and must be accompanied by a session key.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Katya,

    I added the session key code for the file name (we already have it for saving the file, right?). But I'm still getting the error, because I'm not sure how to change the mapping/variables for the datasource after doing that.

    Thank you,
    Anna
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Anna,

    Please specify the sequence of actions. Is there the following code localStorage.getItem('token') when you call userData()?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Katya,

    Yes, I think so. Here is the current code:

    var file;
    function fileSelectHandler(e) {
    var files = e.target.files || e.dataTransfer.files;
    file = files[0];
    previewFile();
    }

    function userData(filename) {
    var url = 'https://api.appery.io/rest/1/db/collections/things/';
    $.ajax({
    type: "POST",
    beforeSend: function(request) {
    request.setRequestHeader("X-Appery-Database-Id", "51c1d0bde4b0d0a692ef56b1");
    request.setRequestHeader("X-Appery-Session-Token", localStorage.getItem('token')),
    request.setRequestHeader("Content-Type", "application/json");
    },
    url: url,
    data: {"filename": filename},
    processData: false,
    success: function(html) {
    alert("it worked");
    },
    failure: function(html) {
    alert("it didn't work");
    }
    });
    }

    function upload() {

    var serverUrl = 'https://api.appery.io/rest/1/db/files/' + file.name.replace(/ /g, "_");

    $.ajax({
    type: "POST",
    beforeSend: function(request) {
    request.setRequestHeader("X-Appery-Database-Id", "51c1d0bde4b0d0a692ef56b1"),
    request.setRequestHeader("X-Appery-Session-Token", localStorage.getItem('token')),
    request.setRequestHeader("Content-Type", file.type);

    },
    url: serverUrl,
    data: file,
    processData: false,
    contentType: false,
    success: function(html) {
    // upload successful
    $(".content").html(html);
    $('#success').show();

    userData(html.filename);
    },
    error: function(html) {
    // upload unsuccessful
    $(".content").html(html);
    $('#error').show();
    }
    });
    }

    function previewFile() {

    var previewContainer = $('table[dsid=preview]');
    var fileName = $('[name=fileName]');
    // make the preview container visible once a file was selected
    previewContainer.toggle();

    // set the file name
    fileName.text(file.name);

    // display image in preview container
    if (file.type.indexOf("image") == 0) {
    var reader = new FileReader();
    reader.onload = function(e) {
    var image = $('[name=image]');
    image.attr('src', e.target.result);
    $('[class=mobileimage1_div]').show();
    }
    reader.readAsDataURL(file);
    }

    }


    Basically, we're using the upload image tutorial to upload a file to the database, but we also want to upload form data (with the file name given by the file database) to the object database once the file upload is successful. We tried to implement what the database docs had in order to communicate between the file and object databases, but that didn't work. When we added the token to the request header, it didn't change the error we were getting. We also tried adding the service to the page that users will upload from and got the same error.

    Thanks,
    Anna
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Let's check what data you send in userData()
    In the beginning, run the following:alert( filename );
    Is this the correct string? Or is this a more complicated object?
    If this is a usual string then please share your app with support@appery.io and tell us the app name.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Katya,

    Adding the alert showed the file name as it shows up in the file database. I shared the file with support@appery.io. Our app is called "installers".

    Thanks,
    Anna
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Replace
    data: {"filename": filename},
    with the following:
    data: JSON.stringify( {"filename": filename} ),
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Katya,

    I replaced the first code with the second and now I get a different error: {"code":"DBSC216","description":"Emptyobjectscan'tbecreated.Pleasespecifyatleastonefield."}.

    We also added an alert for the stringify function and it gave us the correct output.

    var file;
    function fileSelectHandler(e) {
    var files = e.target.files || e.dataTransfer.files;
    file = files[0];
    previewFile();
    }

    function userData(filename) {
    alert( JSON.stringify( {"filename": filename} ) );
    var url = 'https://api.appery.io/rest/1/db/collections/things/';
    $.ajax({
    type: "POST",
    beforeSend: function(request) {
    request.setRequestHeader("X-Appery-Database-Id", "51c1d0bde4b0d0a692ef56b1");
    request.setRequestHeader("X-Appery-Session-Token", localStorage.getItem('token')),
    request.setRequestHeader("Content-Type", "application/json");
    },
    url: url,
    data: JSON.stringify( {"filename": filename} ),
    processData: false,
    success: function(html) {
    alert("it worked");
    },
    failure: function(html) {
    alert("it didn't work");
    }
    });
    }

    function upload() {

    var serverUrl = 'https://api.appery.io/rest/1/db/files/' + file.name.replace(/ /g, "_");

    $.ajax({
    type: "POST",
    beforeSend: function(request) {
    request.setRequestHeader("X-Appery-Database-Id", "51c1d0bde4b0d0a692ef56b1"),
    request.setRequestHeader("X-Appery-Session-Token", localStorage.getItem('token')),
    request.setRequestHeader("Content-Type", file.type);

    },
    url: serverUrl,
    data: file,
    processData: false,
    contentType: false,
    success: function(html) {
    // upload successful
    $(".content").html(html);
    $('#success').show();

    userData(html.filename);
    },
    error: function(html) {
    // upload unsuccessful
    $(".content").html(html);
    $('#error').show();
    }
    });
    }

    function previewFile() {

    var previewContainer = $('table[dsid=preview]');
    var fileName = $('[name=fileName]');
    // make the preview container visible once a file was selected
    previewContainer.toggle();

    // set the file name
    fileName.text(file.name);

    // display image in preview container
    if (file.type.indexOf("image") == 0) {
    var reader = new FileReader();
    reader.onload = function(e) {
    var image = $('[name=image]');
    image.attr('src', e.target.result);
    $('[class=mobileimage1_div]').show();
    }
    reader.readAsDataURL(file);
    }

    }
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Is there any way to fix this new error?

    Thanks,
    Anna
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • 1
    Hi Anna,

    I'm sorry for the delay.

    What are you seeing in the alert( JSON.stringify( {"filename": filename} ) );?

    Do you have field `filename` in your collection?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • I’m thankful
    Hello Anton,

    That's completely fine - I just wanted to make sure the topic didn't get buried!

    The alert gives {"filename":"nameoffile.jpg"} with "nameoffile.jpg" being the name of the file in the database.

    No, we didn't. We didn't know we had to. But this fixed our error!!! Thank you so much for your help (Anton and Katya)!

    Thanks again,
    Anna
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi
    I am also getting same error
    code":DBSC007", "description":"serializationerror

    I am trying to access a rest service of Appery form Google Chrome rest client

    This is my service url : https://api.appery.io/rest/1/db/colle...

    I am providing data base id over there but not the session id because it is not required for a collection.

    Pls help!
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Asif,

    Do you try on preview on on service Test tab? Do you use our proxy?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Katya,
    I tried it on Appery service test tab, it is working fine from there with the same parameters and same settings. I am not using Appery.io proxy
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • when i am doing a GET operation it is working fine from Google chrome rest client, but it is failing when i m selecting POST option
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Asif,

    Please post a screenshot of your Chrome REST client where we can see a request itself.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Katya,
    Sorry my bad. Actually I was sending request data in wrong format that's why error was coming.
    Its working fine now.

    thanks!
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited