nightmare
nightmare copied to clipboard
Running 2x asynchronous is not working
I've been trying a lot of different ways to run the nightmare 2 times asynchronous and nothing worked.
This is my current code:
const Nightmare = require('nightmare');
var bluebirdNightmare = Nightmare({
Promise: require('bluebird')
});
// #1. Set our product
const ourProductUrl = 'https://www.mywebsite.co.uk;
// #2. Get the name
const scanMyEcommerce = url => {
console.log('#1', url);
return bluebirdNightmare
.goto(url)
.evaluate(() => document.querySelector('h1[itemprop="name"]').textContent)
.end()
.then(productName => { return productName; })
.catch(error => console.log('Search failed:', error));
}
// Treat the name to be a good search term
const covertoToSearchTerm = content => {
console.log('#2', content);
// Remove html tags
content = content.replace(/<[^>]*>?/gm, '');
// Remove extra spaces
content = content.replace(/\s+/g, ' ').trim();
// Remove hifens with spaces around
content = content.replace(/\s-\s/g, ' ');
// Remove parentedes
content = content.replace(/([()])/g, '');
// content = content.replace(')', '');
return content;
}
// #3. Get competirors on Google
const searchCompetitors = keyword => {
console.log('#3', keyword);
return bluebirdNightmare
.goto('https://www.google.com')
.type('form[role="search"] input[title="Search"]', keyword)
.click('form[role="search"] input[aria-label="Google Search"]')
.wait('#rhscol')
.evaluate(() => [...document.querySelectorAll('cite')].map(e => e.closest('a').href))
.end()
.then(foundUrls => { return foundUrls; })
.catch(error => console.log('Search failed:', error));
}
// Run the robot
scanMyEcommerce(ourProductUrl)
.then(term => {
const searchTerm = covertoToSearchTerm(term);
return searchCompetitors(searchTerm);
})
.then(res => {console.log('#4', res);});
The code console.log '1', '2' and '3' but not '4' How should I fix it? What I'm doing wrong?