Search code examples
javascriptstackmob

Fetching data from Stackmob using javascript


I am a beginner to javascript and stackmob. How can I fetch data from stackmob for my webpage(dashboard) using javascript? I was also trying to update data but i was unsuccessful.

Here is my code for updating the value of 'done' from 'false' to 'true' :

    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://static.stackmob.com/js/stackmob-js-0.9.1-bundled-min.js"></script>
    </head>
    <body>
    <script type="text/javascript">
    StackMob.init({publicKey: "my_key",apiVersion: 0});
    </script>
    <script type="text/javascript">
    var newtask = new task({ newtask_id: 'my_id_primarykey' });
    newtask.save({done:true},{
    success: function(model, result, options) {
    console.debug(model.toJSON());}
    error: function(model, result, options) {}});
    </script>
    <!-- rest of the code -->
    </body>

I need to be able to create an instance(that i have been able to do), update values, delete values and fetch values.

I am not able to update and save multiple values of an existing instance to stackMob.

          StackMob.init({publicKey:"mykey",apiVersion:0});
var Task=StackMob.Model.extend({schemaName:'taskdevice'});
var task1=new Task({task_device_id:'my_id'});
task1.save({imei:'112',name:document.getElementById('e2').value),dob:(document.getElementById('e3').value),email:(document.getElementById('e4').value),phone:(document.getElementById('e5').value)},{
success: function(model, result, options) {
alert("success");
        console.debug(model.toJSON());},
    error: function(model, error, options) {alert("Failure");}
});

It is failing everytime if i try to update multiple values.


Solution

  • You had a few syntax errors in your code there such as the comma between your success and error callbacks. Also, you need to define your Model before trying to save/read data. Your task variable wasn't defined. So you extend the Model to define what schema it should be related to.

    Here's a jsFiddle with a working example: http://jsfiddle.net/wyne/eATur/

    You should also check out the JavaScript Developer Guide on StackMob's developer center.

    // Initialize StackMob object with your public key
    StackMob.init({
        publicKey: "api_key", apiVersion: 0
    });
    
    // Define Model and associate it with a Schema
    var Task = StackMob.Model.extend({ schemaName: 'task' });
    
    $(document).ready(function () {
    
        // Create new instance of the Model with id of object to update
        var updateTask = new Task({ "task_id": "INSERT_OBJECT_ID" });
    
        // Update the task with done=false and save it to StackMob
        updateTask.save({ done: true }, {
            success: function(model, result, options) {
                console.log( "Updated object!" );
                console.log( model.toJSON() );
            },
            error: function(model, error, options) {
                console.log( "Error updating" );
            }
        });
    });