Backendless Support
 
Solved

Getting error "Backendless.XMLHttpRequest is not a constructor" when running Business Logic

I have no idea what the problem is.

My code:

  1. /*Created on 08/22/2017 11:59:34.*/
  2. class MyService {
  3. /**
  4. * @param {String} name
  5. */
  6. helloWorld(name) {
  7. return new Promise((resolve, reject) => {
  8. var queryBuilder = Backendless.DataQueryBuilder.create();
  9. getIngredients( queryBuilder );
  10. function getIngredients( queryBuilder ) {
  11. Backendless.Persistence.of("Ingredients").find( queryBuilder )
  12. .then( handleResult )
  13. .catch( handleError );
  14. }
  15. function handleResult( ingredients ) {
  16. printIngredients( ingredients );
  17. if( ingredients.length > 0 ) {
  18. queryBuilder.prepareNextPage();
  19. getIngredients( queryBuilder );
  20. }
  21. else {
  22. console.log( "Reached the end of collection" );
  23. }
  24. }
  25. function handleError( error ) {
  26. console.log( "Server reported an error - " );
  27. console.log( error.message );
  28. console.log( error.errorCode );
  29. return "E-"+error.message;
  30. }
  31. function printIngredients( ingredientsCollection ) {var ingredients = [];
  32. var ing = "";
  33. for( var i in ingredientsCollection )
  34. ing = ing + ingredientsCollection[ i ].namePrint + ",";
  35. var data = [];
  36. for (var i = 0; i < ing.length; i++){
  37. data.push(ing.charCodeAt(i));
  38. }
  39. var savedFile = Backendless.Files.saveFile( "testfolder", "test.txt", data, true)
  40. .then( function( fileURL ) {
  41. resolve(fileURL);
  42. })
  43. .catch( function( error ) {
  44. reject(error.message);
  45. });
  46. }
  47. });
  48. }
  49. }
  50. Backendless.ServerCode.addService( MyService );

Best Answer
photo

Hi Sean

As workaround I can propose you this one:

  1. const directoryPath = 'my-directory/and-sub-directory'
  2. const fileName = 'my-cool-file.txt'
  3. const data = ['hello', 'there']
  4. const content = JSON.stringify(data)
  5. return Backendless.Request
  6. .put(`${Backendless.appPath}/files/binary/${directoryPath}/${fileName}?overwrite=true`)
  7. .set({ 'Content-Type': 'text/plain' })
  8. .send(Buffer.from(content).toString('base64'))

1c76bf3dba4e58d045ba749f260acf6a

Regards, Vlad

Leave a Comment

Comments (5)

photo
1

Hi Sean,

Does the same service call work when you run the service in the debug mode?

Regards.

Mark

photo
1

No i get the same error

photo
photo
1

Hi, Sean,

I've reproduced this error. I'm sure the problem is in the method saveFile, not in your code.

I've created the internal ticket (BKNDLSS-15887). We will let you know as soon as it is fixed.

Regards Ilya

photo
3

Hi Sean

As workaround I can propose you this one:

  1. const directoryPath = 'my-directory/and-sub-directory'
  2. const fileName = 'my-cool-file.txt'
  3. const data = ['hello', 'there']
  4. const content = JSON.stringify(data)
  5. return Backendless.Request
  6. .put(`${Backendless.appPath}/files/binary/${directoryPath}/${fileName}?overwrite=true`)
  7. .set({ 'Content-Type': 'text/plain' })
  8. .send(Buffer.from(content).toString('base64'))

1c76bf3dba4e58d045ba749f260acf6a

Regards, Vlad

photo
1

We have fixed it in the new 4.1.0 version of JS-SDK