How can I pass a variable into the then clause of a promise - aurelia

I think I must have a fundamental misunderstanding somewhere. I'm using Aurelia to upload files to the server. The first call creates a folder for the files and when that finishes I want to upload the files. My code looks like this:
if(myFiles.length !== 0 && myFiles !== null){
this.data.createContainer(refNo)
.then((response) => {
this.data.upLoadFiles(refNo, myFiles)
});
At the if statement myFiles contains the file list. However when it gets to the this.data.upLoadFiles statement, myFiles is undefined. I'm not sure the right way to do this.

I fell victim to a less than complete understanding of promises. The code to clean up after saving was executing before the promise was resolved.

Related

Flux File upload validation - file type

I am new to reactive programming. I am using flux for file upload. I need to make sure that all the files uploaded are of a specific type. If not I need to fail the request.
File.flatmap(input-> validate file())
.flatMAp(output->uploadtoazur())
My problem is when the second file is unacceptable type the first file has been processed. I want validateFile to scan all file and then do processing further
Basically if you want to process all the files at a time, not one-by-one, you should first collect them, since you're dealing with Flux. So, you can achieve it with collectList() on your Flux
And then, having List of your files, you can validate and process them. Here is an example of making your validation with handle()
On your Flux of files:
.collectList() // collect all files to List
.handle((files, sink) -> {
// validate all your files here, for example, using regular Stream API with allMatch()
...
if (allValid) {
// return these files if all files are valid
sink.next(files);
} else {
// throw Exception if some files are not valid
sink.error(new Exception("Some files are not valid"));
}
})
...
// further processing
This is one of the many possible ideas how to achieve what you want.
P.S. Actually you should have provided more code and format it properly.

In OfficeJS if we use the addFileAttachmentAsync function for multiple files, the callback runs only for one

The basic implementation of the addFileAttachmentAsync is as follows:
Office.context.mailbox.item.addFileAttachmentAsync(
fileURL,
selectedAttachment.name,
{},
(result) => {
console.log('error or success')
},
);
Now this works. If I call this multiple times for multiple files, it works. All the files get uploaded. But one problem. The callback gets fired only once. Let's say there were two files and both files throw errors. I'll be able to read the error of only one file. How can I fix this and make the callback run for all executions separately?

Where the statusFiles should be saved?

As mentioned here, I tried to use Status Files with the below code:
install(StatusPages) {
statusFile(HttpStatusCode.NotFound, HttpStatusCode.Unauthorized, filePattern = "#.html")
}
and saved the file as below:
But after running it, I got the standard 404 error, which means the file are not seen, do I need to add some thing, or I'm saving them in wrong place?
It looks like statusFiles() does a resolveResource(path), so it is looking at: src/main/resources/ and in order to make it understand/see the statusFiles folder, which is lying inside the resources folder, the filePattern is required to be tuned little nit to reflect it, so the correct code is:
statusFile(HttpStatusCode.NotFound, HttpStatusCode.Unauthorized, filePattern = "statusFiles/#.html")

Google Apps Script ScriptDb permissions issue

I am having an issue trying to query the ScriptDb of a resource file in Google Apps Script. I create a script file (file1), add it as a resource to another script file (file2). I call file1 from file2 to return a handle to its ScriptDb. This works fine. I then try to query the ScriptDb but have a permissions error returned.
Both files owned by same user and in same google environment
See code below:
file 1:
function getMyDb() {
return ScriptDb.getMyDb;
}
file 2 (references file1):
function getDataFromFile1() {
var db = file1.getMyDb(); // This works
var result = db.query({..............}); // This results in a permissions error!
}
I am at a loss to understand why I can access file1 and get back a handle on the ScriptDb, but then am not able to query it, due to an permissions issue.
I have tried to force file1 to require re-authorization, but have not yet been successful. I tried adding a new function and running it, so any suggestions there would be gratefully received.
Thanks in advance
Chris
There appears to be an error in file1/line2. It says "return ScriptDb.getMyDb;" but it should say "return ScriptDb.getMyDb();"
If you leave out the ()s then when you call file1 as a library, file1.getMyDb() will return a function which you store in var db. Then the line var result = db.query({..............}) results in an error because there is no method "query" in the function.
Is that what's causing your error?
I have figured out what the problem was, a misunderstanding on my part regarding authorisation. I was thinking of it in terms of file permissions, when in fact that problem was that my code was not authorised to run the DbScript service. As my code calls a different file and receives back a pointer to a ScriptDb database it is not using the ScriptDb service, so then when it calls the db.query() it invokes the ScriptDb service, for which it is not authorised.
To resolve this I just had to create a dummy function and make a ScriptDb.getMyDb() call, which triggered authorisation for the service. The code then worked fine.
Thanks for the input though.
Chris

How to Implement callback for file downloading?

I wrote a script that downloads file from web using file URL. I have an ActiveXObject of following type.
var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
objHTTP.open("GET", strFileURL, false);
It works perfect for small size file says, file size less than 100MB. But when I try to download file with size greater than 100MB my script hanged. Then I tried,
objHTTP.open("GET", strFileURL, true);
but in this case we have to implement a callback function. I don't know how to implement callback and then use it. Can somebody help me. I am using TestComplete 7. Script that I wrote;
var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
objHTTP.open("GET", strFileURL, true);
objHTTP.onreadystatechange = Callback;
objHTTP.send();
while((objHTTP.readyState != 4) && (objHTTP.readyState != 'complete'))
{
Delay(100);
}
if(200 != objHTTP.Status)
{
Log.Error("The " + strFileURL + " file was not found." + " The returned status is " + objHTTP.Status);
return;
}
I don't know how to implement Callback function. Can somebody provide me implementation?
Thanks
Probably, the hanging is the result of the while loop waiting for a specific value of the readyState property. If the property never gets one of the expected values, the script will work forever.
I think the MSXML2.XMLHTTP object fails to load the large file, and never sets the readyState to one of the values your script expects. To understand what exactly is happening, I would check what value the property has after very long time, which is enough either for the file to load, or for the attempt to fail (say, 2 hours). If you know what value the readyState property has when the downloading fails, you can handle it in the script to avoid hanging.
That's it about the hanging itself. Now about the cause of the file downloading problem. I have found a page that tells about the problem and suggests setting higher timeouts - take a look:
http://edgylogic.com/blog/downloading-large-files-vbscript/
The example is in VBScript, but it should be easy to implement the same approach with JScript. Please note that the example uses a different COM object - ServerXMLHTTP. You can read about it (including differences from XMLHTTP) here:
http://msdn.microsoft.com/en-us/library/ms762278(v=VS.85).aspx
I hope this helps.