unable to save an object using nodejs

I am a parse user , hoping to switch to backendless and was trying out to save object using nodejs.Here is the script that i have tried out:
exports.run = function ( request, response )
{
var items = [{“product”:“mobile”,“price”:2.76,“quantity”:3}];

var shoppingItem = new ShoppingItem(items,response);

var failureCallback = function( fault )
{
response.status( 500 ).send( fault );
};

Backendless.Persistence.of( ShoppingItem ).save( shoppingItem, new Backendless.Async(
function( savedOrder )
{
response.send( savedOrder );
}, failureCallback ) );
/* Backendless.Persistence.save( shoppingItem, new Backendless.Async(
function( savedItem )
{
response.send( savedItem);
}, function( fault )
{
response.status( 500 ).send( fault );
response.send( fault );
} ) );/
/
response.send( shoppingItem );*/ //Am getting this object when I run, I guess only save method is not woriking…
};
function ShoppingItem( args,response )
{
args = args || {};
this.___class = ‘ShoppingItem’;
this.product = args[0].product || “”;
this.price = args[0].price || 0.0;
this.quantity = args[0].quantity || 0;
/response.send(args[0].product+"");/

}
Am getting an error:
{“code”:6020,“message”:“Script has timed out before producing a response. Script URL - /repo/web/scripts/hello.js”}
There might be some thing wrong with the code because such a small script cannot throw timed out error.
Can any one help me out?

Hi Prathyusha,

Your model’s first argument should be the object describing an entity, not an array:

function ShoppingItem( args,response )
{
args = args || {};
this.___class = 'ShoppingItem';
this.product = args.product || "";
this.price = args.price || 0.0;
this.quantity = args.quantity || 0;
/*response.send(args.product+"");*/
}

So also you’ll need to change this

var items = [{"product":"mobile","price":2.76,"quantity":3}];
var shoppingItem = new ShoppingItem(items,response);

to this:

var item = {"product":"mobile","price":2.76,"quantity":3};
var shoppingItem = new ShoppingItem(item,response);

And by the way, you don’t need to pass response to your model (I believe you did it just in debugging purposes).

Hi Sergey Chupov,

Thank you for the reply.Yea I have passed the response to the model for debugging.

Iam still getting the same error.Actually there is no problem with object saving, object is getting saved but its not sending the saved item in the response.Am not sure about the issue but may be the call back is not getting triggered…

exports.run = function ( request, response )
{
var item = {“product”:“mobile”,“price”:2.76,“quantity”:3};

var shoppingItem = new ShoppingItem(item);

var failureCallback = function( fault )
{
    response.status( 500 ).send( fault );
};

/*Backendless.Persistence.of( ShoppingItem ).save( shoppingItem, new Backendless.Async(
    function( savedOrder )
    {
        response.send( savedOrder );
    }, failureCallback ) );*/
Backendless.Persistence.save( shoppingItem, new Backendless.Async(
    function( savedItem )
    {
        response.send( savedItem);
    }, function( fault )
    {
       response.send( fault );
    } ) );

/* response.send( shoppingItem );*/
};

function ShoppingItem( args )
{
args = args || {};
this.___class = ‘ShoppingItem’;
this.product = args.product || “”;
this.price = args.price || 0.0;
this.quantity = args.quantity || 0;
/response.send(args[0].product+"");/

}

ERROR:

{“code”:6020,“message”:“Script has timed out before producing a response. Script URL - /repo/web/scripts/hello.js”}

But object is getting saved.

The error indicates that your scripts runs longer than 5 seconds - which is the amount of time allowed for custom code to run in the free tier.

The example shopping chart application is working that is not throwing any error.
My custom code is much smaller compared to that. Will such a small script take more than 5 seconds?

Could you attach your hello.js file to this topic?

Attached is hello.js file

Attached is hello.js

hello .js.zip (553B)

Here’s working version for you. The problem was in the Backendless.Persistence.save call. You missed the of( ShoppingItem ) part:

function ShoppingItem( args )
{
args = args || {};
this.___class = ‘ShoppingItem’;
this.product = args.product || “”;
this.price = args.price || 0.0;
this.quantity = args.quantity || 0;
/response.send(args[0].product+"");/

}

exports.run = function ( request, response )
{
var item = {“product”:“mobile”,“price”:2.76,“quantity”:3};

var shoppingItem = new ShoppingItem(item);


Backendless.Persistence.of( ShoppingItem ).save( shoppingItem, new Backendless.Async(
    function( savedItem )
    {
        response.send( savedItem );
    }, 
    function( fault )
    {
       response.status( 500 ).send( fault );
    } ) );

};

Am still getting the same error.But we tried it from different account the code is working.Even the code that I have shared is working.The problem was with my account.I have created the account using mozilla which raised a security alert box saying your details will be viewed by third party.I have cancelled the request even then It logged me in.

Thank you for considering the problem and immediate support.Please look into the problem with sigining up using mozilla.The dashboard also doesn’t work properly in mozilla public url is not getting copied.