$window.open called in a promise breaks the second promise - angularjs

I am pretty new to angularjs. Love it but sometimes get surprises that I just can't find an answer.
I have a function that lets the user build a list to a file by clicking a link. This can take up to a minute so the user can build different lists at the same time. The meaning is that once the file is build a popup appears to download it.
All of this works when only 1 file is build. After a half a minute the download popup shows up. But when 2 are build at the same time the second POST is cancelled (it arrives in the error callback) once the pop-up of the first appears.
If I remove $window.open both POST are finished succesfully and I get both fileId's in the console log.
Clearly the problem lies with $window.open. But why? What am I doing wrong or how can I solve this? Anyone an idea? I have been searching for hours already but can't seem to find anything on this case.
$scope.idPromise = $http.post(restPath, $scope.data)
.then(function(data){
console.log(data.data);
console.log(data.data.fileId);
$window.open(restPath + 'files/' + data.data.fileId, '_self');
},function(error){
console.log('promise error')
});
So when two lists are build without window.open in the code I get something like this in my console:
POST http://localhost:8080/xxx-rest/rest/lijsten/vastelijsten/gezinshoofdenMetLeden 200 OK 1m
POST http://localhost:8080/xxx-rest/rest/lijsten/vastelijsten/gezinshoofdenMetLeden 200 OK 1m 40s
report3545743463669473959.xlsx
report4733168386603499105.xlsx
when window.open is placed in the code:
POST
http://localhost:8080/xxx-rest/rest/lijsten/vastelijsten/gezinshoofdenMetLeden
200 OK 1m
POST
http://localhost:8080/xxx-rest/rest/lijsten/vastelijsten/gezinshoofdenMetLeden
x 56,39s
report1588183186872251177.xlsx
promise error
As you can see the last POST is stopped as soon as the first POST gets a response, so for the second POST no response is gotten.
Update
It seems the problem lies with the grunt server and the liveReload Protocol. My college had always told me to ignore this error but it is now clear that because the page loses connection that the promise fails.
Does anyone have experience with grunt and liveReload? Can I fix this? Will it work on a production server?
The console error I get:
De verbinding met ws://localhost:35729/livereload werd onderbroken
tijdens het laden van de pagina. this.socket = new
this.WebSocket(this._uri);
(translated: the connection with... was interrupted)

Related

AngularJS testing with Protractor- tests that used to pass now failing due to Waits timing out

About 6 months or so ago I started developing an automated test suite for an AngularJS application developed by my company, using Protractor.
After I had been working on this for a couple of months, some other work came up which I had to prioritise over the test suite, and so I haven't looked at it since around the end of November/ December last year.
When I stopped working on it, I made sure that everything that I had done up to that date was in a fully working state (commented/ removed the tests I had started working on but hadn't finished, etc), and committed that git branch. At this point, I was able to run all of the tests I had written up until then using the command protractor conf.js, and I could see that they were all passing as expected.
I recently checked my testing branch out again, as I have a day or two in between other projects, and thought I could make use of the time by working on the testing again.
The first thing I did once I had checked out the testing branch, was to try running my test scripts again, to ensure everything I had implemented so far was still working.
However, while most of the tests do still pass, a few of them are now failing due to time outs, even though I had made sure that all of the timing elements were working correctly before I shelved the testing for a while.
I have tried increasing the times that my tests should sleep or wait for things at the points at which they are failing, but this doesn't seem to have made a difference.
The particular tests that are now failing due to time outs are:
1.
it('should navigate to the Config/Platform page & check the values are all correct', function() {
browser.waitForAngularEnabled(false);
browser.actions().mouseMove(configMenuBtn).perform();
browser.wait(EC.visibilityOf(pageConfigPlatformBtn), 8000).then(browser.sleep(5000).then( /*ERF(14/11/2017 # 1630) browser.sleep() required to give DialogMgr service time to complete */
pageConfigPlatformBtn.click().then(function(){
browser.sleep(10000); /*ERF(14/11/2017 # 1640) This line is needed- because of how the form HTML is created, it needs time to be replaced by configured HTML that is displaying the required fields */
var eth0Mode = element(by.model('modelinstances.platform.eth_0_mode'));
var eth0Address = element(by.model('modelinstances.platform.static_ip.eth_0_address'));
var eth0Netmask = element(by.model('modelinstances.platform.static_ip.eth_0_netmask'));
var eth0gateway = element(by.model('modelinstances.platform.static_ip.eth_0_gateway'));
var eth1mode = element(by.model('modelinstances.platform.eth_1_mode'));
var eth1Address = element(by.model('modelinstances.platform.static_ip.eth_1_address'));
var eth1netmask = element(by.model('modelinstances.platform.static_ip.eth_1_netmask'));
var eth1gateway = element(by.model('modelinstances.platform.static_ip.eth_1_gateway'));
expect(browser.getCurrentUrl()).toMatch(moxaConfigPlatformUrlRegExpPattern);
expect(eth0Mode.getAttribute('value')).toBe("Static IP");
expect(eth0Address.getAttribute('value')).toBe("192.168.1.127");
expect(eth0Netmask.getAttribute('value')).toBe("255.255.255.0");
expect(eth0gateway.getAttribute('value')).toBe("192.168.1.1");
expect(eth1mode.getAttribute('value')).toBe("Static IP");
expect(eth1Address.getAttribute('value')).toBe("192.168.2.127");
expect(eth1netmask.getAttribute('value')).toBe("255.255.255.0");
expect(eth1gateway.getAttribute('value')).toBe("");
})));
})
The failure message for this test is:
App should navigate to the Config/Platform page & check the values are all correct
Message:
Failed: Wait timed out after 8002ms
Stack:
TimeoutError: Wait timed out after 8002ms
2.
it('should navigate to the Config/Date/Time page', function() {
browser.waitForAngularEnabled(false);
browser.actions().mouseMove(configMenuBtn).perform();
browser.wait(EC.visibilityOf(pageConfigDateTimeBtn), 2000).then(browser.sleep(1000).then( /*ERF(14/11/2017 # 1630) browser.sleep() required to give DialogMgr service time to complete */
pageConfigDateTimeBtn.click().then(function() {
expect(browser.getCurrentUrl()).toBe(VM + '/#/config/systemtime');
})));
})
The failure message for this test is:
App should navigate to the Config/Date/Time page
Message:
Failed: Wait timed out after 2023ms
Stack:
TimeoutError: Wait timed out after 2023ms
3.
it('should navigate to the Tag Browser page (final test)', function() {
console.log("Start final Tag Browser page test");
browser.waitForAngularEnabled(false);
browser.wait(EC.visibilityOf(pagesMenuBtn), 10000).then(
browser.actions().mouseMove(pagesMenuBtn).perform().then(
browser.wait(EC.visibilityOf(pageConfigDateTimeBtn), 2000).then(browser.sleep(1000)).then( /*ERF(14/11/2017 # 1650) browser.sleep() required to give DialogMgr service time to complete */
browser.wait(EC.visibilityOf(pageTagBrowserBtn), 12000).then(
pageTagBrowserBtn.click().then(
function() {
console.log("Tag Browser menu button clicked");
}).then(
browser.wait(EC.visibilityOf(tagBrowserPageTagsLink), 20000).then(
function(){
console.log("End Tag Browser page test (then call)");
expect(browser.getCurrentUrl()).toBe(VM + '/#/pages/tagbrowser');
}
)
)
)
)
)
);
});
The failure message for this test is:
App should navigate to the Tag Browser page (final test)
Message:
Failed: Wait timed out after 2009ms
Stack:
TimeoutError: Wait timed out after 2009ms
I have tried increasing the times that the wait() calls are being passed, but this hasn't resolved the issue.
I have read in the past that automated testing can be quite flakey, and that changes to the environment in which they're run can cause them to fail- so I'm guessing it's possible that because my computer will have changed since the tests were last run successfully (i.e. new software installed), this may be causing the tests to fail...?
Is there a method of 'best practice' for resolving this sort of issue with automated testing, or is just a case of having to go back and tweak my test scripts until they start passing again?
It's probably worth mentioning that all of my test are written in a spec.js file, and that these tests which failed are the last 3 of 18 test scripts in that file (i.e. the first 15 all still pass).
Anyone have any ideas how I can resolve this/ get my tests passing again?

JSON post, browser and application different times

Before i start eating my keyboard, maybe someone can help me out here...
Im using Angular Bootstrap to set a time. After posting the time, and checking my console, i see that the selected time is posted. But after checking my log file, i see that the time is posted minus 1 hour. I've set my app => timezone to Europe/Amsterdam (ECT if im right) but still cant find what is going wrong.
Can someone help me please?
Controller:
public function update( EventRequest $request, $eventGuid )
{
Log::info($request->all());
$this->event->updateById($request, $eventGuid);
}
Angular post will be done as: 2016-01-27 03:45:39
Console log: 2016-01-27 03:45:39
Laravel log: 2016-01-27 02:45:39

Spontaneous Server Errors During AngularJS $http calls

I'm building an SPA in AngularJS served by a Laravel (5.1) backend. Of late I've been encountering an annoying error, a server 500 or code 0 error which is abit hard to explain how it comes but let me try to may be someone will understand the dental formula of my problem.
When i start my AngularJS controller, I make several server calls (via independent $http calls from services) to retrieve information i might later need in the controller. For example,
Functions.getGrades()
.then(function(response)
{
$scope.grades = response.data;
});
Subjects.offered()
.then(function(response)
{
$scope.subjects = response.data;
});
Later on i pass these variables (grades or subjects) to a service where they are used for processing. However, these functions are randomly returning code 500 server errors after they run, and sometimes returning status code 0 after running. This happens in a random way and it is hard for me to point out the circumstances leading to their popping up. This leaves me with frequent empty Laravel-ised error screens like the ones shown below.
Anyone reading my mind?
Ok, after a suggestion given in a comment above that I check my Laravel log files (located in storage/logs/laravel.log- Laravel 5.1), i found out that the main error most of these times was this one: 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge'' in ..., plus another one that paraphrased something like No valid encrypter found. These were the key opener.
On reading another SO thread here, it said in part:
I solved, sometimes laravel not read APP_KEY in .ENV. And returns a value "SomeRandomString" (default is defined in config / app.php), and have the error "key length is invalid", so the solution is to copy the value of APP_KEY, to the value 'key 'in config / app.php, that's all! I solved!
That was exactly the issue! When loading the DB params from the .env to config/database.php, Laravel was sometimes unable to read the environment variables and went for the fallback default fallback options (forge for DB name and username and SomeRandomString for the APP_KEY). So, to solve this i just did as advised: copied the APP_KEY in .env to the config/app.php and edited the default DB parameters to the actual DB name and username/password I'm using. Just that and i was free from pollution. Hope someone finds this helpful.

Weird (intermittent) Can't set headers after they are sent error

Have a very strange intermittent problem, and one that I just can not understand at all. Not certain it is code related, bug in express or just me missing something.
Have an app, all in MEAN, that like so many other apps around does a bunch of API calls. Some even in parallel.
It all worked perfect this morning, actually, read the "where it gets weird", but then all of a sudden it stops working and the server starts falling over with the below error:
GET /api/skillList 304 45.345 ms - -
_http_outgoing.js:335
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/Users/bengtbjorkberg/WebstormProjects/ResourceEdge/node_modules/express/lib/response.js:695:10)
at ServerResponse.json (/Users/bengtbjorkberg/WebstormProjects/ResourceEdge/node_modules/express/lib/response.js:232:10)
at /Users/bengtbjorkberg/WebstormProjects/ResourceEdge/routes/api.js:78:9
at /Users/bengtbjorkberg/WebstormProjects/ResourceEdge/node_modules/mongoose/node_modules/kareem/index.js:160:11
at Query._findOne (/Users/bengtbjorkberg/WebstormProjects/ResourceEdge/node_modules/mongoose/lib/query.js:1145:12)
at /Users/bengtbjorkberg/WebstormProjects/ResourceEdge/node_modules/mongoose/node_modules/kareem/index.js:156:8
at /Users/bengtbjorkberg/WebstormProjects/ResourceEdge/node_modules/mongoose/node_modules/kareem/index.js:18:7
at process._tickCallback (node.js:355:11)
Process finished with exit code 1
What I have checked.
Threw tons of log messages, looks like it falls over at different places, or I am missing one of the places
Spent a lot of time looking at the last call (/api/skilllist), to be working quite OK.
Where it gets REALLY weird
If I start the developer console in Chrome, the problem DOES NOT OCCURE, which is probably why it worked all day but then stopped working when I wanted to show it to someone...
While I was writing this, I realised that I could use safari, and it falls over with the java console on, and it is the same line server side. And its the database call below. But if I turn the console on in Chrome, it starts working... What am I missing
exports.canlist = function (req, res) {
// use mongoose to get all profiles in the database
console.log("Canlist called");
Profile.find( {}, {'_id':1, 'alias':1, 'img':1, 'summary':1, 'keys':1}, function(err, profiles) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err) {
console.log("Error " + err)
res.send(err)
}
console.log("Sending back " + profiles.length + " profiles")
res.json(profiles); // return all todos in JSON format
});
};
So, after much faffing about, I figured it out. Not why it works when using java console it open on Chrome.
Basically the res.send(err) command is wrong.
First its missing return.
Secondly I THINK you are suppose to use the longer form like this:
return res.status(500).send(err);
return res.json(profile);
Think the problem was that it was confusing express, which probably do some pre processing, which may explain why it "seems" to blow up at all kind of times.
(Would have kept quiet about this, as it is a bit of a ridiculous mistake, but it saves someone a few hours staring at the screen I might get a christmas card this year. (or at least get to tell santa that I've been a good boy)

404 after 43 seconds TTFB

I have script which uses simple_html_dom to parse different site data. It looks through my table of users, grabs the various sites needed, and then parses the data and stores them into my db.
The problem is that when I iterate through more than 3 users I get a 404 error. After a lot of debugging (much of which I'm learning as I go) it looks like as soon as my TTFB hits 40 seconds I get a 404 not found error. Anything under that the page returns fine.
I included the following in my php file to extend the time but this problem seems to ignore these statements.
// It may take a whils to crawl a site ...
ini_set("memory_limit", "-1");
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
ini_set('max_input_time', -1); //300 seconds = 5 minutes
set_time_limit(0);
But I've never had this problem before where I get a 404 for a page that exists. I'm somewhat new to simple_html_dom and crawling through different pages but is the problem that the wait time is too long? If so how do I can I fix that? Thanks
So it did not have to do with execution time or any setting I could change with the php script. For anyone having the same issue this was fixed by changing the way simple_html_dom loads the script from:
$html = new simple_html_dom();
$html->load_file($url_link);
To:
$html = #file_get_contents($url_link);
$html = str_get_html($html);
Hope this helps someone else!

Resources