AngularJS SDK: Can't Create Records


#1

Hi,

CreateRecords() API on a Remote SQL DB does nothing. It doesn’t throw errors, nor is there any backend call. It seems like it gets lost in some infinite loop inside the SDK’s JS File.

My code is as below:-

  var request={};
  request.table_name="test";
  request.body={};
  request.body.record=[];
  testDetail={};
  testDetail.name="Here I come";
  request.body.record.push(testDetail);
  DbService.createRecords(request).then(
    function(result){
      console.log(result);
    },function(reject){

    }
  );

My effective request object is:-
{
“table_name”:“test”,
“body”:
{
“record”:
[
{“name”:“Here I come”}
]
}
}

When I fire the above in the API, nothing happens.
This is the code which calls inside the DbService:-
createRecords:function(request){
console.log(JSON.stringify(request));
return DreamFactory.api.sql.createRecords(request);
}

Can you please check your SDK file and see if the request goes through fine at your end.
I will make changes accordingly.


#2

Hi Praveen,

NOTE: THIS ANSWER IS INCORRECT AS IT RELATES TO REMOTE WEB SERVICES AND NOT REMOTE SQL SERVICES

It’s not that ‘it does nothing’. It’s that you haven’t defined what it does yet. :smile: The methods contained in the SDK are generated by a Service Definition. From your listed code I’m assuming you have a Remote SQL DB service called ‘sql’. If you were to open the service definition tab on the services page for this service you would find only a base definition. This contains no methods or ‘CreateRecords()’ functions or api. You have to define these for your custom service. This is all based on Swagger. We currently use the 1.2 spec. Click here to find out more about the Swagger 1.2 spec and defining a service(api) through swagger.

I would consider this an intermediate to advanced topic. To make things a tad bit easier you can copy the definition from our ‘db’ service and paste it into the service definition for your ‘sql’ service. Don’t forget to save your service after doing so. This should give you the result your looking for. You may have to clear the swagger cache which is the wrench icon button on the Launchpad button bar at the top. One other piece of advice I can give you when you do this is to not edit the definition at all. The DSP will populate it with the proper service names and routes.

As an aside and for anybody else reading this…I’m not 100% sure how things will react without the proper models being defined for the service. Once again…this is a more advanced topic than i can go into here and knowledge of Swagger is a must to fully take advantage of defining services like this.


#3

That’s surprising!

I have the following question then:-

  1. how do I see the a long list of APIs against my service SQL on the Live API Docs page on my DSP? How does it already show the methods?
  2. How does getrecords( ) works without any setup from my end but create records fails?

I assume that seeing the list of Apis in the live docs that something already exists in my DSP against my service Sql.

Please clarify these points, as they will assist my understanding.


#4

I tried looking at what you suggested.

However, this is what I see:-

  1. On clicking on the Service Definition, I see the following:-
    This service definition is not editable
    Hence, the Create New and Reset buttons are not enabled.

  2. Secondly, when I see the default service definition in there, I see a long list of methods with their corresponding definitions. One of them being createRecords as well. This is how it looks in there.

     {
       "method": "POST",
       "summary": "createRecords() - Create one or more records.",
       "nickname": "createRecords",
       "notes": "Posted data should be an array of records wrapped in a <b>record</b> element.<br/> Use the <b>related</b> parameter to return related records for each resource. By default, no related records are returned.<br/> By default, only the id property of the record is returned on success. Use <b>fields</b> parameter to return more info.",
       "type": "RecordsResponse",
       "event_name": [
         "{api_name}.{table_name}.insert",
         "{api_name}.table_inserted"
       ],
       "parameters": [
         {
           "name": "table_name",
           "description": "Name of the table to perform operations on.",
           "allowMultiple": false,
           "type": "string",
           "paramType": "path",
           "required": true
         },
         {
           "name": "body",
           "description": "Data containing name-value pairs of records to create.",
           "allowMultiple": false,
           "type": "RecordsRequest",
           "paramType": "body",
           "required": true
         },
         {
           "name": "fields",
           "description": "Comma-delimited list of field names to retrieve for each record, '*' to return all fields.",
           "allowMultiple": true,
           "type": "string",
           "paramType": "query",
           "required": false
         },
         {
           "name": "id_field",
           "description": "Single or comma-delimited list of the fields used as identifiers for the table, used to override defaults or provide identifiers when none are provisioned.",
           "allowMultiple": true,
           "type": "string",
           "paramType": "query",
           "required": false
         },
         {
           "name": "id_type",
           "description": "Single or comma-delimited list of the field types used as identifiers for the table, used to override defaults or provide identifiers when none are provisioned.",
           "allowMultiple": true,
           "type": "string",
           "paramType": "query",
           "required": false
         },
         {
           "name": "continue",
           "description": "In batch scenarios, where supported, continue processing even after one record fails. Default behavior is to halt and return results up to the first point of failure.",
           "allowMultiple": false,
           "type": "boolean",
           "paramType": "query",
           "required": false
         },
         {
           "name": "rollback",
           "description": "In batch scenarios, where supported, rollback all changes if any record of the batch fails. Default behavior is to halt and return results up to the first point of failure, leaving any changes.",
           "allowMultiple": false,
           "type": "boolean",
           "paramType": "query",
           "required": false
         },
         {
           "name": "X-HTTP-METHOD",
           "description": "Override request using POST to tunnel other http request, such as DELETE.",
           "enum": [
             "GET",
             "PUT",
             "PATCH",
             "DELETE"
           ],
           "allowMultiple": false,
           "type": "string",
           "paramType": "header",
           "required": false
         },
         {
           "name": "related",
           "description": "Comma-delimited list of relationship names to retrieve for each record, or '*' to retrieve all.",
           "allowMultiple": true,
           "type": "string",
           "paramType": "query",
           "required": false
         }
       ],
       "responseMessages": [
         {
           "code": 400,
           "message": "Bad Request - Request does not have a valid format, all required parameters, etc."
         },
         {
           "code": 401,
           "message": "Unauthorized Access - No currently valid session available."
         },
         {
           "code": 404,
           "message": "Not Found - Resource not found"
         },
         {
           "code": 500,
           "message": "System Error - Specific reason is included in the error message"
         }
       ]
     },
    

I am not sure what else needs to be done. From what I understand and see, it seems everything is setup well.
Can you please try and see if Angular SDK runs file at your end. Can you please test CreateRecords( ) at your end once, with a remote SQL DB (Mine is PostGreSQL).


#5

Sorry. Scratch that. I was thinking about Remote Web Services. The definition for sql databases is already there. Are you getting an error? What is the response code from the server when you run the function?


#6

No error nothing.
As I said in my first post, no call gets made from the SDK.
It goes into some infinite loop in your SDK Js file.

Please check at your end with the JS file. It seems it’s not tested.


#7

I’m looking into it right now. We’ve made many calls with the SDK to different databases. What makes you think that it’s an infinite loop? Does your browser crash or lock up?


#8

Yes, it seems to be in an infinite loop, without making any network calls.
I can check all the network calls using chrome’s developer tools, and I don’t see anything happening.

When I pause the execution using the debugging tool, I see the code pausing somewhere around the Line 969 in the angular-dreamfactory.js file. The code snippet is as below:-

if (valuesArr.length > 1) {
for (var i = 0; i < valuesArr.length - 1; i++) {
if (valuesArr[i].isDefault) {
params.headers[param.name] = valuesArr[i].value
}
}
}

As I try debugging it, I see the loop going on and on. In the code above I see the length = 9, but it gets called again and again with different number of arg I guess.
I am not an expert in it, so cant comment, but I know for sure that nothing happens and it gets stuck in the SDK file.

Please let me know if there is a way, in which I can chat live with you or show you my screen or something like that. If needed, I can call you as well. If that helps.


#9

I found it. An inner loop was re initializing the outer loops counter. Which is strange because we’re able to build an run other commands. I’ve tested a fix and it seems to be working. I should be able to push a bug fix for you by noon. You 'll just have to run ‘update bower’ in your project and everything should be good.


#10

Can you please try to call a remote SQL CreateRecords. If you can at your end, then it might be something wrong at my end. If you can’t then I am sure must be something wrong at SDK end.

Why I am asking about testing is that because, its not that nothing works, for e.g getRecords( ) works fine, however createRecords fails, so it could be that some APIs have not been tested.
That’s why I was suggesting testing this one as well.


#11

Ok wonderful.

Since I am a bit new to this bower thing, so, I just need to take the latest Angular SDK file that is the angular-dreamfactory.js file.
Please let me know if that is not the understanding.


#12

yes. Basically that’s it. Are you not using bower and yeoman to build your projects?


#13

yeah I guess I use bower :smile:
I just copy the instruction and forget :smiley:

I am new to the Development world, so I stick to what is my area and ignore the larger stuffs.


#14

Ok. You should be able to grab the new dreamfactory-angular.js from the github repo or via bower. I’m able to create records now. Please give it a shot and let me know if everything works out for you.


#15

Wonderful… Yes createRecords( ) creates records well now.

The next thing I am going to use is Update and Delete.
I hope that works fine too and does not have any error.

Thank You !!