How can we make synchronous calls using JavaScript app42 APIs

0 votes
Hi,

We are using the App42 JavaScript api. but we want to call the methods synchronously as we are assigning success result of one function to a global variable.

Eg. we have an image to be uploaded and saved in App42, then we get its url and store it in another json object using insertJSON function.

Since the functions are working asynchronously now, we have to insert the main json first, get its doc id, upload the image, then save the image url in main JSON by calling an update method(update by docId). so basically we have an over head of updating the json only for one parameter (i.e the image url).
asked Dec 28, 2015 in App42 Cloud API-BaaS by jitesh.h.lalwani (20 points)

1 Answer

0 votes

Hello Jitesh,

In order to answer your query, instead of calling two api to insert image URL along with insertJSONDocument method. You can call insertJSONDocumentWithFile method to save image url, please find below code snippet for the same and let me know if you need any help from my side:

var storage  = new App42Storage();
var dbName = "database name";
var collectionName = "collection name";
var filePath = document.getElementById("filePath");
var name = "file name";
var file = filePath.files[0];
var fileJSON = new Object();
fileJSON.name = name;
fileJSON.type = "IMAGE";
fileJSON.filePath = file; // File Path of your image which you need to upload
var result ;
storage.insertJSONDocumentWithFile(dbName, collectionName, "{\"name\":\"Himanshu\"}",fileJSON,{
success: function(object) {
result = JSON.parse(object);
console.log(result.app42.response)
},
error: function(error) {
 
}
}); 
 
 
Regards,
Himanshu Sharma
answered Dec 30, 2015 by hs00105 (2,005 points)
Hi,

Thanks for the reply.

Do we have something for updating json with file as well?

Also, can we make the function calls synchronous? We also have a case where we need to store images in a loop and then we need to get each image's url and save it in json
Hello Jitesh,

Apologies for getting late back to you, JavaScript SDK defaults support Asynchronous API model. What you can do on the UI side, you can create loader in upload case. Once all of your files uploaded successfully, you can update your document using addOrupdateDocument method by passing the document id of your existing document and list of your uploaded files in the form of JSONObject/JSONString. Please have a look at this (http://api.shephertz.com/app42-docs/nosql-storage-service/#add-or-update-keys-with-json-string) link for the code snippet and let me know if it helps.

regards,
Himanshu Sharma
Also, you can write one recursive function which upload all of your files in the success of each response. Once your files uploaded successfully, you can update your existing document which is stated in above comment.

Regards,
Himanshu Sharma
Hi,
We are getting an error saying "{"app42Fault":{"httpErrorCode":400,"appErrorCode":2605,"message":"Bad Request","details":"Passed JSON string is not valid."}}" with method insertJSONDocumentWithFile in Javascript.
Below is the code :

var imageFileType = "IMAGE";
posterFile = document.getElementById("poster").files[0];

var fileJSON = new Object();
fileJSON.name = fileName;
fileJSON.type = imageFileType;
fileJSON.filePath = posterFile;
   
storageService.insertJSONDocumentWithFile(dbName, collectionName, movieJSON,fileJSON,{    
        success: function(object)   
        ...

movieJSON is the actual JSON to be inserted. Please help.
Hi Jitesh,

Could you please share movie JSON code with us. It will help us to provide better support to you.

Regards,
Himanshu Sharma
Himanshu,

Please see the movieJSON and fileJSON below:

Movie JSON:
{
   "title":"sample movie",
   "synopsis":"",
   "genre":"",
   "poster":"",
   "trailer":"",
   "directors":[
      "dir"
   ],
   "producers":[
      "prod"
   ],
   "writers":[
      "writer"
   ],
   "username":"abcd",
   "insertDate":"Thu Feb 11 2016 12:14:55 GMT-0600 (Central Standard Time)",
   "updateDate":"Thu Feb 11 2016 12:14:55 GMT-0600 (Central Standard Time)",
   "releaseDetails":[

   ],
   "starcast":[
      {
         "actorName":"",
         "characterName":""
      }
   ],
   "facts":[

   ],
   "quiz":[

   ]
}

fileJSON:
{
   "name":"samplemovie",
   "type":"IMAGE",
   "filePath":"D:/poster.jpg"
}
Hi,
Please update on this.

Thanks.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...