1

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.

Ice
  • 35
  • 1
  • 1
  • 6
  • Any error message? Also, that space in `stackmob-js-   0.9.1-bundled-min.js`, is it really present in your code? – acdcjunior Jun 29 '13 at 19:45
  • @acdjunior: How do we see any error message in this? In the server console? – Sohaib Jun 29 '13 at 19:52
  • @SilentPro Browser console. Try pressing F12. – acdcjunior Jun 29 '13 at 20:00
  • @acdcjunior: error is "failed to load resource". the spaces are not in the program, they were typing mistakes here. – Ice Jun 29 '13 at 20:17
  • I don't know StackMob, but your code seems to have some syntax errors. Are you sure that `task` you are trying to create an object from exists? Where did you get this code from? – acdcjunior Jun 29 '13 at 20:51
  • could be `new Todo(...` I saw it int this link: https://developer.stackmob.com/js-sdk/developer-guide – acdcjunior Jun 29 '13 at 20:55
  • @acdcjunior: the schema newtask has been created. i have also referred to the same link. – Ice Jun 30 '13 at 14:14

1 Answers1

3

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" );
        }
    });
});
Justin
  • 71
  • 3
  • Thanks a lot. Your code creates a new instance. I took help from the developer guide of stackmob(the link that you gave) and am now able to update values. The code in jsFiddle does not produce the result as expected(maybe I am not able to use the jQuery properly) but I will take help from there and work it out. :) – Ice Jun 30 '13 at 14:13
  • Sorry, I updated the snippet to do an update instead of create. Not sure why the fiddle wouldn't work. It works for me. You just have to hit the create button first, then the update button. But I'm glad you got it working! – Justin Jul 01 '13 at 01:53
  • Thanks a lot :) now i am able to work all the crud operations! – Ice Jul 02 '13 at 03:12