In Protractor I'm trying to run a function from another page inside my spec file.
My spec file:
let TablePage = require("./../pages/TablePage");
let table_page = new TablePage();
let protractor = require("protractor");
let browser = protractor.browser;
describe('Login', function() {
beforeEach(function() {
browser.ignoreSynchronization = true;
browser.waitForAngular();
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
browser.get("/budget");
});
it('should get values from list', function(){
table_page.getPriceValuesFromList();
});
The other file form which i get the function (TablePage.js):
let protractor = require("protractor");
let browser = protractor.browser;
let number = 0;
let prices = [];
let TablePage = (function () {
function TablePage() {
}
TablePage.prototype.getPriceValuesFromList = function () {
for (number = 1; number < 100; number++) {
let locator = '//*[#id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
browser.findElement(By.xpath(locator)).then(function (err) {
prices[number] = element(By.xpath(locator)).getText();
console.log(prices[number])
}, function (err) {
if (err) {
break;
}
})
}
};
return TablePage;
});
module.exports = TablePage;
I get an error: table_page.getPriceValuesFromList is not a function
Do you know what's wrong? I was doing it this way in the other project and it was working.
The real typeof() this function is undefined
You can also check if the function will work - it should get values from one row of the table, save it in array and go to the next row until the value inside row is not found - Save values form the column
There are a couple of lines which shouldn't be there. TablePage should be defined as follows:
function TablePage() { }
// Removed lines which were here.
TablePage.prototype.getPriceValuesFromList = function () {
for (number = 1; number < 100; number++) {
let locator = '//*[#id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
browser.findElement(By.xpath(locator)).then(function (err) {
prices[number] = element(By.xpath(locator)).getText();
console.log(prices[number])
}, function (err) {
if (err) {
break;
}
})
}
};
// And removed the return TablePage();
As an object orientalist I prefer implementation using classes:
class TablePage {
getPriceValuesFromList() {
for (number = 1; number < 100; number++) {
let locator = '//*[#id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
browser.findElement(By.xpath(locator)).then(function (err) {
prices[number] = element(By.xpath(locator)).getText();
console.log(prices[number])
}, function (err) {
if (err) {
break;
}
})
}
};
};
Related
I am trying to trigger an another function in Firebase Cloud function with javascript. But i always getting an error of Can't set headers after they are sent. Please take a look at my code below: ................. ................. ............ ................ ................. ............... ....................... .................. ..............
exports.productIndexShuffleOne = functions.https.onRequest(async (req, res) => {
const interval = req.query.interval;
console.log("interval: "+interval);
const productRef = admin.firestore().collection("Products");
const adminRef = admin.firestore().collection("Admin").doc("totalProd").get();
const dateRef = admin.firestore().collection("Admin").doc("totalProd").collection("indexShuffle").doc("productShuffle").get();
return dateRef.then(documentSnapshot => {
const setDate = documentSnapshot.get('date').seconds;
var nextDay = setDate;
console.log("Date: "+nextDay);
const x = setInterval(function() {
clearInterval(x);
return Promise.all([adminRef]).then(result => {
const totalNum = result[0].data().totalNumber;
console.log("totalNum: "+totalNum);
var numberList = [];
var index = 1;
while(index <= totalNum){
numberList.push(index);
index++;
}
var cidx, ridx, tmp;
cidx = numberList.length;
while (cidx !== 0) {
ridx = Math.floor(Math.random() * cidx);
cidx--;
tmp = numberList[cidx];
numberList[cidx] = numberList[ridx];
numberList[ridx] = tmp;
}
console.log(numberList);
var counter = 0;
return productRef.get().then(snapshot => {
snapshot.forEach(doc => {
const prodID = doc.get('productID');
const index = doc.get('index');
var newIndex = numberList[counter];
counter++;
console.log("oldIndex: "+index);
console.log("newIndex: "+newIndex);
productRef.doc(prodID).update({
index: newIndex
}, {merge: true});
});
return res.redirect('https://us-central1-myfunction-123456.cloudfunctions.net/productIndexShuffleTwo?interval='+interval);
})
.catch(err => {
console.log('Error getting documents', err);
});
});
}, interval);
return res.status(203).send(interval);
}).catch(function(err) {
console.error(err);
});
});
This is because you've sent multiple responses while the rule is that you only allowed sending one response. Please try to look at your code and optimize it in such a way that it contains only one response.
I can see you have multiple responses as below:
1 -> return res.redirect('https://us-central1-myfunction-123456.cloudfunctions.net/productIndexShuffleTwo?interval='+interval);
2 -> return res.status(203).send(interval);
I believe that you can have res.redirect and then res.status.send called one after another. When you writing endpoints there rule of a thumb: always send response and only do that once. Refactor your code so there no way you can make those two calls, but only one of them.
I am trying to access the values of the map outside of the function getbetlist like do a console.log(bettingPool) outside of that function but whenever I do that it just prints {}. However inside the function getbetlist it prints the correct output.
I have tried changing the varible types, setting the var, creating a new var and resetting it and nothing has worked thus far.
var db = firebase.firestore();
var bettingPool = {}
var marketSplit = {}
var weightedMarketSplit = {}
var msToNextDay = 0;
var test;
async function startNewGame() {
await getBetList();
var now = new Date();
var nextDay = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1,
7, 59, 59
);
test();
msToNextDay = nextDay.getTime() - now.getTime();
// console.log(msToNextDay)
// bettingPool = {}
// marketSplit = {}
// weightedMarketSplit = {}
// setTimeout(startNewGame(), msToNextDay);
}
startNewGame();
async function getBetList() {
console.log("inside getBetList")
const users = []
const accuracy = []
db.collection('Bets').get().then(
function (querySnapshot) {
querySnapshot.forEach(
function (doc) {
doc.data().userDoc.onSnapshot(docSnapshot => {
/* Betting Pool Split
bettingPool - By Money
marketSplit - By Users
weightedMarketSplit - By users * accuracy
*/
if (!bettingPool[doc.data()['stockId']]) {
bettingPool[doc.data()['stockId']] = {}
marketSplit[doc.data()['stockId']] = {}
weightedMarketSplit[doc.data()['stockId']] = {}
}
if (doc.data()['direction'] == 'Up') {
if (!bettingPool[doc.data()['stockId']].Up) {
bettingPool[doc.data()['stockId']].Up = 0.0
marketSplit[doc.data()['stockId']].Up = 0
weightedMarketSplit[doc.data()['stockId']].Up = 0.0
}
marketSplit[doc.data()['stockId']].Up += 1
bettingPool[doc.data()['stockId']].Up += Number(doc.data()['bet'])
weightedMarketSplit[doc.data()['stockId']].Up += (docSnapshot.data()['accuracy'])
}
else if (doc.data()['direction'] == 'Down') {
if (!bettingPool[doc.data()['stockId']].Down) {
bettingPool[doc.data()['stockId']].Down = 0.0
marketSplit[doc.data()['stockId']].Down = 0
weightedMarketSplit[doc.data()['stockId']].Down = 0.0
}
marketSplit[doc.data()['stockId']].Down += 1
bettingPool[doc.data()['stockId']].Down += Number(doc.data()['bet'])
weightedMarketSplit[doc.data()['stockId']].Down += (docSnapshot.data()['accuracy'])
}
test = bettingPool;
})
}
)
}
)
// for (var user of users) {
// db.collection("Users").doc('' + user).onSnapshot(querySnapshot => {
// // if(querySnapshot.data()['accuracy']) {
// // accuracy.push(querySnapshot.data()['accuracy'])
// // }
// // else {
// // accuracy.push(0.5);
// // }
// // console.log(querySnapshot.data())
// })
// }
}
It's kind of funky code to modify a global object inside a function, for multiple reasons. Especially because the function is called "getBetList" but it doesnt get anything, it just updates a variable somewhere else. Its much cleaner to do something like
const bettingPool = await getBetList()
And have getBetList return the betList.
This should also fix your problem.
I am trying to read some data from 2 different tables and parse a CSV file before rendering an ejs file.
I can get the data from both tables and from the CSV file but I seem to be unable to return the result.
Pretty sure this is a problem with the way I handle async execution but I fail to see what I am doing wrong.
I've spent the last 2 days reading about this (including the threads around here) and browsing but somehow the answer still escapes me.
First file - usercms.js
app.get('/userscms', function(req, res)
{
existingUsers.getExistingUsers()
.then(function(appUsers)
{
//global users array
//I can display these in my ejs file
globalAppUsers = appUsers;
})
.then(existingUsersAttributesQlik.getExistingUsersAttributesQlik())
.then(function(usersQlikAttributes)
{
//global user attributes array
//undefined data
globalUsersQlikAttributes = usersQlikAttributes;
})
.then(existingSuppliers.parseSuppliersCSV())
.then(function(supplierData)
{
//the result I am expecting
//this prints undefined
console.log(supplierData);
}).then(function()
{
res.render('userscms.ejs',
{
users: globalAppUsers,
attributes: globalUsersQlikAttributes
});
});
});
Second function - getxistingUsers.js (identical to the getExistingUsersAttributesQlik, except for the query)
var userData = [];
var appUsers = [];
(function (exports)
{
exports.getExistingUsers = function ()
{
return promisemysql.createConnection(dbconfig.development).then(function(conn)
{
var result = conn.query("SELECT id, username, firstName, lastName, email, phone, lastLogin, isAdmin, isValid, isPhoneValid, accountCreationDateTime FROM Users");
conn.end();
return result;
}).then(function(rows)
{
return rows;
}).then(function(rows)
{
if (rows.length)
{
userData = [];
appUsers = [];
rows.forEach(function (elem)
{
userData.push(_.toArray(elem));
});
for (i = 0; i < userData.length; i++)
{
var appUser = new appUserModel.AppUser(
userData[i][0],
userData[i][1],
userData[i][2],
userData[i][3],
userData[i][4],
userData[i][5],
userData[i][6],
userData[i][7],
userData[i][8],
userData[i][9],
userData[i][10]);
appUsers.push(_.toArray(appUser));
}
return appUsers;
}
else
{
console.log("NOPE");
return null;
}
}).then(function(appUsers)
{
console.log(appUsers);
return appUsers;
});
};
})(typeof exports === 'undefined' ? this['getExistingUsers'] = {} : exports);
Third file - parseSuppliersCSV.js
var supplierData = [];
var suppliersData = [];
var csvCount = 0;
(function (exports)
{
exports.parseSuppliersCSV = function ()
{
return new Promise(function(resolve, reject)
{
var fileStream = fs.createReadStream("myCSV.csv");
var parser = fastCsv();
csvCount = 0;
supplierData = [];
suppliersData = [];
fileStream
.on("readable", function ()
{
var data;
while ((data = fileStream.read()) !== null)
{
parser.write(data);
}
})
.on("end", function ()
{
parser.end();
});
parser
.on("readable", function ()
{
var data;
while ((data = parser.read()) !== null)
{
if(csvCount >= 1)
{
csvCount++;
var arrayOfStrings = data[0].split(';');
var supplier = new supplierModel.Supplier(arrayOfStrings[0],arrayOfStrings[1]);
suppliersData.push(_.toArray(supplier));
}
else
{
csvCount++;
}
}
})
.on("end", function ()
{
console.log("done");
//all OK here
console.log(suppliersData);
//this doesn't seem to return anything
return suppliersData;
});
});
};
})(typeof exports === 'undefined' ? this['parseSuppliersCSV'] = {} : exports);
Any ideas what I am doing wrong? Am I approaching this the wrong way?
I'll take a guess here and assume the promise you created should resolve to something...instead of returning a value.
.on("end", function ()
{
console.log("done");
//all OK here
console.log(suppliersData);
//this doesn't seem to return anything
return resolve(suppliersData);
});
I have one function getUsers where I have one array jsonResponse. I am passing that array to computeData function. I want computeData function should be able to add items in jsonResponse array itself.
But below code is not adding in same array, as it always return empty array in response.send function.
index.js
exports.getUsers = functions.https.onRequest((request, response) => {
var x = [];
var xLocation,
yLocation = [],
jsonResponse = [];
// print algorithm name
console.log(request.query.algo);
let geoFire = new GeoFire(db.ref("/users/" + request.query.userId));
geoFire.get("location").then(function(location) {
xLocation = location;
});
db
.ref("/users/" + request.query.userId)
.once("value")
.then(function(snapshot) {
var jsonObject = snapshot.val();
var basicProfileJsonObject = jsonObject.basicProfile;
for (var key in basicProfileJsonObject) {
if (utils.isNumber(basicProfileJsonObject[key])) {
x.push(basicProfileJsonObject[key]);
}
}
db.ref("/users/").once("value").then(function(snapshot) {
var y = [];
snapshot.forEach(function(item) {
var user = item.val();
let userId = user.basicProfile.userId;
if (userId !== request.query.userId) {
if (xLocation == null) {
computeData(x, user, request.query.algo, jsonResponse);
} else {
let geoFire = new GeoFire(db.ref("/users/" + userId));
geoFire.get("location").then(function(location) {
if (location === null) {
console.log(
"Provided key is not in GeoFire, will ignore profile"
);
computeData(x, user, request.query.algo, jsonResponse);
} else {
console.log("Provided key has a location of " + location);
var distance = GeoFire.distance(xLocation, location); // in km
console.log("Distance: " + distance);
if (distance < 15) {
computeData(x, user, request.query.algo, jsonResponse);
}
}
});
}
}
});
response.send(jsonResponse);
});
});
});
function computeData(x, user, algo, jsonResponse) {
var similarityCount,
y = [];
var basicProfileJsonObject = user.basicProfile;
for (var key in basicProfileJsonObject) {
if (utils.isNumber(basicProfileJsonObject[key])) {
y.push(basicProfileJsonObject[key]);
}
}
if (algo === "cosine") {
// compute cosine value
similarityCount = cosineUtils.cosineSimilarity(x, y);
} else if (algo == "euclidean") {
// compute euclidean distance value
similarityCount = 1 / (1 + euclidean(x, y));
} else if (algo === "pearson-correlation") {
// compute pearson correlation coefficents
similarityCount = pcorr.pearsonCorrelation(x, y);
}
console.log(x);
console.log(y);
console.log(similarityCount);
jsonResponse.push(user);
}
Does anyone know how to pass array as reference and add items into it in Cloud Function for Firebase ?
Your else statement is a promise which means your loop would have finished and called response.send(jsonResponse); by the time it gets to computeData() in your else statement.
Try something like this, it doesn't touch all your variables but the main idea is to use Promise.all with computed values as resolved -
exports.getUsers = functions.https.onRequest((request, response) => {
// blah blah
var y = []; // store promises that resolves your computed value
snapshot.forEach(function(item) {
// blah blah
if (xLocation == null) {
y.push(Promise.resolve(computeData());
} else {
y.push(computeAnotherData(userId));
}
});
Promise.all(y)
.then(values => {
response.send(values);
});
});
function computeAnotherData(userId) {
let geoFire = new GeoFire(db.ref("/users/" + userId));
return geoFire.get("location").then(function(location) {
return computeData();
});
}
Hope it makes sense.
I'm current trying to use sails.js with mongodb, I need some custom mapReduce function to group data.
Now I could achieve what I want by using waterline's native function, but have some questions.
These function has only small variation actually, but I found myself keep repeating codes like the following one:
function getSomeData() {
// First-query
Log.native(function(err, logCollection) {
var mapFunction = function() {
function dateFormatter(date) {
return date.getFullYear() + "-" + (date.getMonth() + 1)
}
//! Generate Grouping Key
emit(dateFormatter(this.emb_date), this.bad_qty)
}
var reduceFunction = function (key, values) {
return Array.sum(values);
}
var outputControl = {
out: {inline: 1},
//! Filters
query: {order_type: product}
}
logCollection.mapReduce(mapFunction, reduceFunction, outputControl, function (err, result) {
if (err) {
callback(err);
return;
}
var resultSet = [];
//! post-processing
for (var i = 0; i < result.length; i++) {
//.....
}
callback(err, resultSet);
});
});
}
Second-query:
function getAnotherData() {
Log.native(function(err, logCollection) {
var mapFunction = function() {
//! Generate Grouping Key
emit(dateFormatter(this.product), this.bad_qty)
}
var reduceFunction = function (key, values) {
return Array.sum(values);
}
var outputControl = {
out: {inline: 1},
//! Filters
query: {order_type: product}
}
logCollection.mapReduce(mapFunction, reduceFunction, outputControl, function (err, result) {
if (err) {
callback(err);
return;
}
var resultSet = [];
//! post-processing
for (var i = 0; i < result.length; i++) {
//......
}
callback(err, resultSet);
});
});
}
As you can see, these two snippet shares lots of common code, only has difference in three place (Generate grouping key, filters, post-process).
So I would really like to extract the common part to make my code cleaner, but have no success.
I first try to make dateFromatter is provided by a callback instead of hard-coding like the following:
function dateFormatter(data) {
return data.emb_date.getFullYear() + "-" + (data.emb_date.getMonth() + 1)
}
function getSomeData(groupingKey) {
// First-query
Log.native(function(err, logCollection) {
var mapFunction = function() {
//! Generate Grouping Key
emit(groupingKey(this.emb_date), this.bad_qty)
}
var reduceFunction = function (key, values) {
return Array.sum(values);
}
var outputControl = {
out: {inline: 1},
//! Filters
query: {order_type: product}
}
logCollection.mapReduce(mapFunction, reduceFunction, outputControl, function (err, result) {
if (err) {
callback(err);
return;
}
var resultSet = [];
//! post-processing
for (var i = 0; i < result.length; i++) {
//.....
}
callback(err, resultSet);
});
});
}
But without any luck, I keep getting error like the following one:
MongoError: exception: ReferenceError: groupingKey is not defined near 'emit(groupingKey(this), this.bad_qty' (line 3)
at Object.toError (/home/brianhsu/zh800/dashboard/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/utils.js:114:11)
What should I do if I would like to reduce those duplicate part of code?
Finally I found that I need pass the option called 'scope' to mongodb, I come up with the following solution which works quite well.
exports.defineOn = function(options) {
var model = options.model
var groupingFunction = options.groupingFunction
var mongoFilters = options.mongoFilters
var customFilter = options.customFilter
var converter = options.converter
var sorting = options.sorting
return function(callback) {
model.native(function(err, collection) {
var mapFunction = function() { emit(groupingFunction(this), this.bad_qty) }
var reduceFunction = function(key, values) { return Array.sum(values); }
var mapReduceOptions = {
out: {inline: 1},
query: mongoFilters,
scope: {
groupingFunction: groupingFunction,
mongoFilters: mongoFilters,
customFilter: customFilter,
converter: converter
}
}
var processCallback = function (err, result) {
if (err) {
callback(err);
return;
}
if (sorting) {
result.sort(sorting);
}
var resultSet = [];
for (var i = 0; i < result.length; i++) {
if (customFilter && customFilter(result[i])) {
resultSet.push(converter(result[i]));
} else if (!customFilter) {
resultSet.push(converter(result[i]));
}
}
callback(err, resultSet);
}
collection.mapReduce(mapFunction, reduceFunction, mapReduceOptions, processCallback);
});
}
}
Usage:
function machineDetail (year, month, date, machine, callback) {
var startDate = new Date(+year, +(month-1), +date);
var endDate = new Date(+year, +(month-1), (+date) + 1);
var mapReducer = MapReducer.defineOn({
model: Log,
groupingFunction: function(data) {
return {date: data.emb_date, error: data.defact_id};
},
mongoFilters: {
mach_id: machine,
emb_date: {$gte: startDate, $lt: endDate}
},
converter: function (data) {
return {
name: data._id,
value: data.value,
};
}
});
mapReducer(callback);
}