When passing a collection, only the first element seems to be passed - javascript

I am using my code to get data from JIRA. JIRA server returns a set of elements (called issues), and this works perfectly fine:
for (let issue of JiraIssues) {..processing code } //this works well, I get all the issues
However, when I wanted to separate the code getting the data from the code processing it, I cannot figoure out how to pass the JiraIssues correctly:
Processing(JiraIssues) //calling the function
function Processing(jiraIssues) //fucntion signature
The thing is, what is passed into the function seems to be just the first element.

function processOneIssue(issue) {
console.log(issue); // logs one issue
}
function processAllIssues(issueList) {
console.log(issueList); // logs the list with all the issues
}
JiraIssues.forEach((issue) => {
this.processOneIssue(issue);
});
this.processAllIssues(JiraIssues);
I am not sure if I understand your problem. But this two little function should do what you need. Make sure you are calling them in the right place. It is possible that you are overwriting the original JiraIssues inside the loop with one of the element. Try using different less familiar variable names for testing purposes.
UPDATE based on the comment:
//// file1
export function processOneIssue(issue) {
console.log(issue); // logs one issue
}
export function processAllIssues(issueList) {
console.log(issueList); // logs the list with all the issues
}
////// file 2
import * as file1 from 'path1';
JiraIssues.forEach((issue) => {
file1.processOneIssue(issue);
});
file1.processAllIssues(JiraIssues);
There should be no diffrence just because the import.

Related

Overwriting existing object properties

I am having trouble with two small blocks of code and understanding why one works and one doesn't.
I have an array of 'tasks'.
When a user updates a task, I need to update the AuditLog, to show the update.
I have two blocks of code, first one works, second one doesn't.
1st block - when I view the updated task, it shows the updated log.
2nd block - when I view the updated task, the log is not updated.
I suspect that it is something to do with the way I am referencing, assigning things.
This block correctly updates the "task" and viewing it shows the updated "audit log".
//update task with server response 'a'
this.updateTask(task).subscribe(a => {
let updatedTask = initializeTask(a);
task.auditLog = updatedTask.auditLog;
});
Then I thought I could simplify the code a bit...
but when using this block, when I view the task, it does not show the updated log.
(I should mention that printing 'intializeTask(a)' works correctly and prints the updated log values, so it's not to do with that).
//update task with server response 'a'
this.updateTask(task).subscribe(a => {
task = initializeTask(a);
});
I feel like I may be missing something fundamental in the way JavaScript references/assigns variables etc. Perhaps someone can shed some light on it.
Thanks in advance!
PS: initializeTask simply ensures that nothing is left undefined and that dates are formatted correctly:
export function initializeTask(t): Task {
t.subject = t.subject || '';
t.startTime = initDate(t.startTime);
t.state = t.state || 'Published';
t.createdBy = t.createdBy || '';
if (t.auditLog) {
t.auditLog.forEach(d => {
d.createdOn = initDate(d.createdOn);
});
}
return t;
}

Autocomplete in Google Apps Script with data from Google Sheets

I am trying to use Google Apps Script to create a form, where the fields allow for autocompletion. In other forms I’ve created, I’ve been able to pull an array of options from a google sheet, and use them to populate a drop down list, so I have to think it’s possible to do the same with an autocomplete process.
I’ve blatantly copied this example from w3schools, and it works exactly as needed, as long as I declare the array within the javascript (as done in the example). But what I haven’t been able to figure out is how to populate the array with options pulled from my google sheet.
Here is where I started:
var PMOSupport;
$(function() {
google.script.run.withSuccessHandler(buildDropDowns)
.getPMOSupport();
});
function buildDropDowns(data) {
PMOSupport = data;
console.log(PMOSupport);
}
function autocomplete(inp, arr) {
console.log("ENTER AUTO");
var currentFocus;
inp.addEventListener("input", function(e) {
// all of the remaining code is direct from the w3schools example
// I'm cutting it from here for brevity,
// and because I know this works, when using the declared array below
});
}
var countries = ["Afghanistan","Albania","Algeria","Andorra"];
// this line works fine, when using the array declared above
// autocomplete(document.getElementById("myInput"), countries);
// this line does not work, when trying to use the array populated from the google sheet
autocomplete(document.getElementById("myInput"), PMOSupport);
When I run this, the page creates, and as soon as I type into the entry field, I get a message in the console:
`Uncaught TypeError: Cannot read property 'length' of undefined`
at HTMLInputElement.<anonymous> (<anonymous>:32:28)
When I look into this, it’s saying that the ‘arr’ argument (PMOSupport) isn’t populated. That’s why I added the 2 console.log lines, to see what order things are happening. When I open the page, “ENTER AUTO” logs first, then the State Changes from Idle to Busy and Busy to Idle (while it calls getPMOSupport()), then the PMOSupport array logs in the console (also proving that I am in fact getting the correct data back from the sheet). So clearly, it’s entering function autocomplete() before it calls the google.script.run.withSuccessHandler(buildDropDowns).getPMOSupport() portion, which is why the 'arr' argument is undefined.
I’ve tried various ways of taking that out of the $(function() … }); block, to try to get the PMOSupport array populated before the autocomplete() function runs. Nothing I’ve done seems to be working.
I’m sure this is something simple, and caused by bad habits I’ve picked up over time (I’m not a developer, I just cobble things together for my team). But any help would be appreciated.
You need to call autocomplete AFTER the asynchronous code has returned. So, you need to invoke it from the callback.
function buildDropdowns(data, userObject) {
// probably you should indicate in data which field these data is for, or use
// the userObject parameter in the google.script.run API
autocomplete(document.getElementById("myInput"), data);
}
Alternately (I haven't and won't look at the w3schools code), declare your PMOSupport as a const array initially, and then add the entries from your callback into it (instead of reassigning it). This way, the variable is not undefined, it is just empty at the start. Depending on the implementation of the autocomplete code, this may or may not work for you.
const PMOSupport = [];
....
function buildDropdowns(data) {
PMOSupport.push(...data);
// or
// Array.prototype.push.apply(PMOSupport, data);
}

Marklogic Server Side Javascript: XDMP-CONFLICTINGUPDATES while using explicit commit

I've been having problems with conflicting updates in Marklogic. I'm aware of the cause, but I don't know how to fix it.
I have 1 main (.sjs) file which calls two different (.sjs) files which both update a set of documents. In the main file I use: declareUpdate({explicitCommit: true}); and then in the separate files I use the command xdmp.commit(); after updating the documents. However, I'm still getting: XDMP-CONFLICTINGUPDATES.
Part of the code: Main.sjs:
function main(){
declareUpdate({explicitCommit: true});
function.to.file.1();
function.to.file.2();
}
file1.sjs:
//make some docs and insert them into ML
function file1Function(){
for (let d of someCollectionOfData) {
xdmp.documentInsert('/a/uri/filexx.json', d, {collections: aCollectionName1});
};
xdmp.commit();
}
file2.sjs:
//adjust docs made in file1 and put them back into ML
funtion file2Function(){
for (let d of xdmp.directory('/location/of/aCollectionName1/','infinity')) {
let dObj = d.toObject();
dObj.addSomething = 'something';
xdmp.documentInsert(fn.documentUri(d), dObj, {collections: aCollectionName1});
}
xdmp.commit();
}
It must mean your file1 is located inside '/location/of/aCollectionName1/'. Keep in mind that MarkLogic doesn't commit immediately when you invoke xdmp.commit(). Actual persisting is always postponed until after all code has executed. It therefor doesn't make much sense to invoke xdmp.commit() more than once in one request. You won't be able to read your updates after xdmp.commit().
HTH!

JavaScript - how to use Setters and Getters in to create a listener

Before this gets marked as a duplicate: I have read posts all day about this so I know there are tons of similar questions on SO but none that I've seen so far go into the details that I need to understand.
Having said that, there are no good commented examples of how the process works. Could someone answer the following question with well a well-commented example so I could finally understand this ability?
I have a function that I want to call in one file but I need to make sure that another event in another file has already happened before I call it. These files have no connection (one is an angular 2 TypeScript file that starts the app and the other is a JS file that manages a hopscotch tour). I understand that I will need to use a global variable and I believe that the best solution I've read is going to involve using setters and getters. All examples I've seen of this seem to assume that it's just intuitive and leave out the part where I get to understand how it's working. Maybe it is intuitive but I'm not making the leap yet.
Global variable in TypeScript file:
global_variable = false;
Function I want to call in JavaScript file based on the listener:
function call_if_other_function_finishes() {
if (global_variable === true) { // I have the global already created
// run hopscotch tour
}
} // how do I turn this into a listener?
The function I need to have finished first in TypeScript file:
function someFunction() {
// run its code
GlobalFile.global_variable = true; // Should trigger the listener.
}
Thanks in advance!!
One solution is to just define the function as you do in your example and then run it when you need it:
function someFunction() {
// run its code
call_if_other_function_finishes() // it's globally defined anyway
}

Create basic library firebase error

I have developed some applications using Firebase. Over time, I realized that I am repeating a lot of unnecessary code and decided to create a small library to help me increase productivity. Right at the beginning, I tried to create this object in Javascript:
read.childRoot = function(att) {
var acessChildRoot = firebase.database().ref("root/");
acessChildRoot.once('value').then(function(snapshot) {
alert(snapshot.child("nome").val());
});
}
And I tried to access through this line of code:
alert(read.childRoot("nome"));
So I was able to read the reference I wanted, but the first return was the undefined value. How can I filter this value and just display the value I really want to see?
It seems that you want to wait for the first value to be set on a node.
In that case, I recommend using this snippet (from my gist):
var listener = ref.on('value', function(snapshot) {
if (snapshot.exists()) {
console.log('Value is now '+snapshot.val());
ref.off('value', listener);
}
});

Categories

Resources