defining array in pug.js - arrays

I can't understand the difference. What is wrong here? Whenever I write it like this in pug—
-const users =[
{
"key":"Property",
"key":"Property",
}];
—it throws an error. And when I write it like this—
-
const users =[
{
"key":"Property",
"key":"Property",
}];
—it does not throw errors and gives the right result.
Please help me if you know the difference.

Unbuffered code in Pug must either all be on one line, or it must all be indented beneath a hyphen (-), with the hyphen on a line by itself.
// this is ok
- let users = ['foo', 'bar']
// this is also ok
-
let users = [
'foo',
'bar'
]
// this isn't ok
- let users = [
'foo',
'bar'
]
Placing content beside the hyphen signals to Pug that the code is all on one line, and it doesn't treat the next line as code, even if it's indented.

Related

Get readline input as an array

I'm doing a programming challenge right now but I'm struggling with getting the input right. There is no feedback on my output, only "error" which makes it really hard to debug for me. Here is the input:
4 2
1 4
2 9
4 7
5 8
and I want to collect it like this:
[4, 2, 1, 4, 2, 9, 4, 7, 5, 8];
The test environment tells me to work with the input like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (line) => {
var nums = line.split(' ');
/*Solve the test case and output the answer*/
});
How ever I must be getting the wrong array for my nums variable.I tried a bunch of approaches (splitting by /n and whitespaces, iterating with for loop and push... working with rl.close...) but as there is virtually no feedback on my input I am getting kind of desperate here. A simple interface which tells me my program output would help...
SOLUTION
var nums = [];
rl.on("line", line => {
let newLine = line.split(" ");
newLine.map(line => nums.push(line));
});
rl.on("close", function() {
console.log(nums)
});
It was possible for me to debug via the terminal once I got the input right.
The 'line' event would be emitted every time when rl read a new line, so you should just declare your nums outside of the callback function of the listener. Something like this:
...
var sums = [];
rl.on('line', (line) => {
let newNumbers = line.split(' '); // [4, 2]
sums.concat(newNumbers);
});
...
You may want to understand how event emitter and event listener work in JavaScript.
I think there is nothing for you even you register an 'error' event listener because everything is working as expected. And I believe there must be an event like 'end' or 'closed' which would be emitted after rl read all content of the input, and you can console.log your sums array there, I believe you can get your expected result.
You shouldn't handle the sums array right after the closing brackets, JavaScript is asynchronous, so those codes would be executed before all of the lines are read. If there is a method like rl.close you should call it in the situation like:
rl.on('line', (line) => {
...
if (line === undefined) { // or any terminal character which createInterface would return.
rl.close();
}
});
And I believe rl.close() will emit the event like what I said above , something like 'end' or 'closed', put the code to handle the final sums there.
A simple interface which tells me my program output would help...
You can run your nodejs application with nodejs! You can download and install it, and then in your terminal node yourjscodefile.js. When you console.log(variable); it will output in the terminal.

JSON_MODIFY appends variable containing JSON with escape characters instead of JSON string

I have had some success using JSON_MODIFY to append fieldErrors: [] and its content to the root of another object { data: []} so that the resultant object looks something like this: {data: [], fieldErrors: []}.
Problem is, when I append a #variable, it includes a bunch of escape characters.
I expect this: {"name":"PosTitle","status":"the field is messed up, yo"}
BUT, I get this: ["{\"name\":\"PosTitle\",\"status\":\"the field is messed up, yo\"}"]}
DECLARE
#fieldErrors nvarchar(max) ='{}'
,#jsonResponse nvarchar(max) = '
{
"data": [
{
"PosTitle": "",
"PosCode": "86753",
}
]
}
'
--define the fields that are bad
set #fieldErrors = JSON_MODIFY(JSON_MODIFY(#fieldErrors, '$.name', 'PosTitle'), '$.status', 'the field is messed up, yo')
print #fieldErrors
--RESULT, this looks great:
--{"name":"PosTitle","status":"the field is messed up, yo"}
-- append fieldErrors to the response
set #jsonResponse = JSON_MODIFY(#jsonResponse, 'append $.fieldErrors', #fieldErrors)
print #jsonResponse
--RESPONSE, this includes escape characters
/*
{
"data": [
{
"PosTitle": "",
"PosCode": "86753",
}
]
,"fieldErrors":["{\"name\":\"PosTitle\",\"status\":\"the field is messed up, yo\"}"]}
*/
Why are escape characters being added when fieldErrors is appended to the response?
Please remove the comma at the end of "PosCode": "86753", as it's not a valid JSON this way.
To answer your question, you are trying to add a string stored in #fieldErrors which results in the escape characters being added.
Instead,
set #jsonResponse = JSON_MODIFY(#jsonResponse, '$.fieldErrors', JSON_QUERY(#fieldErrors)) should yield the results you're looking for.
Note that when you use append you are telling JSON_MODIFY that it is adding a value to an array (which may or may not be what you need, but isn't what you wrote you're expecting).

Can't Figure Out Correct Protractor Syntax

I'm building Protractor tests and am seeking to refactor some code.
This line of code works fine:
element.all(by.css('div.modal-content ly-input input')).first().sendKeys("Workflow 1");
Then I tried doing this:
var name = element(by.css('div.modal-content ly-input'));
element(name).all(by.css('input')).sendKeys("Workflow 1")
There is something wrong with this last line of code, as I'm getting a "Failed: Invalid locator" error.
Might anyone know how I can introduce the name variable and use it in the last line of code?
Robert
My apologies, I finally figured it out:
name.all(by.css('input')).sendKeys("Workflow 1")
I see you figured it out. Here is how I write my sendKeys in order to have more clarity when I read my code:
var name = 'Workflow 1';
var input = element.all(by.css('div.modal-content ly-input'));
input.first().sendKeys(name);
// or if you have multiple inputs in that element above
var workflowData = {
name: 'Workflow 1',
description: 'a workflow',
type: 'simple'
}
var input = element.all(by.css('div.modal-content ly-input'));
input.get(0).sendKeys(workflowData.name);
input.get(1).sendKeys(workflowData.description);
input.get(2).sendKeys(workflowData.type);

Jade variable from array // not working

I'm passing an array to the jade document. Then I'd like to access the values of the array via a variable to keep the markup simple. Just see the example below. I have already picked up, that the jade syntax can be quite strange dealing with arrays (stuff like "arr.[0]"). Can you guys tell me what im overseeing here? Big thanks!
- var arr = [
{
name: 'foo',
id: 1
},
{
name: 'bar',
id: 2
}
]
- var item = arr[0];
h2 #{item.id} // doesn't work
h2 #{arr[0].id} // works
h2 #{arr[0].id}
Works because you are referencing the id for arr item in the '0' (first) position. This is because the array starts counting things with a zero, not a one.
As you can probably see from your results, this code would return '1', which means that you could expect h2 #{arr[0].name} To return 'foo'.
To get the ids from both items in the array 'arr', change your code to this.
h2 #{arr[0].id}
h2 #{arr[1].id}

Value is not an array ref error

I have a list of checkboxes. I am trying to pass the list of selected checkboxes to a perl script. I am obtaining the list of checkboxes using the folliwng code :
function exec(){
var checkedValue = "";
var inputElements = document.getElementsByTagName('input');
for(var i=0; inputElements[i]; i++){
if(inputElements[i].className==="chk" &&
inputElements[i].checked){
checkedValue += inputElements[i].value;
if (inputElements[i+1])
checkedValue += ", ";
else
checkedValue += "";
}
}
I am then passing "checkedValue" to a perl script as follows :
self.location='/cgi-bin/ATMRunJob.pl?tcs='+checkedValue;
In the perl script, I read the array as follows :
our #testCasesToRun = $var->param("tcs");
This is then assigned to a hash as follows :
my $runSpec = {
TestCasesToRun => #testCasesToRun
};
However, I get the following error when I load the page in the browser :
Failed TestLimits() with error: [hash: k=TestCasesToRun, v=1,]:[array]:Value is not an array ref
In check against following TLS:
[
'hr',
{
'OptDefaults' => {
'JobRunningGroupName' => 'astbluetooth',
'RunMode' => 'Queue',
'CountTowardsReporting' => 1,
'JobOwnerGroupName' => 'astbluetooth',
'SelectSetupTeardown' => 1
},
'Optional' => {
'TestCasesToRun' => [
'ar',
undef,
undef,
[
'r',
1,
undef
]
],
I am new to perl as well as CGI scripting. How could I get around this error?
NOTE : All the code snippets have been shortened for brevity, but still portray the essence of the problem.
EDIT : What I want to do is this. The user selects a list of test cases from a checkboxed list that he wants to execute. I take the test case ids of all the selected test cases and pass it to a perl script. In the perl script, I just need to assign these selected testcase ids to the TestCasesToRun element in the runspec hash.
What would be the correct way to do that?
You are assigning an array as a hashkey value. That doesn't work; you need to assign an array ref:
my $runSpec = {
TestCasesToRun => \#testCasesToRun
};
Given that the code compiles, I have a feeling you just messed up your examples in the Q - please fix them to accurately reflect your code, even if they will be slightly less brief.
Your 'tcs' parameter is a single string (assigned via JS). Why are you then assigning results of param('tcs') to an array in the first place? Do you have a split somewhere in your code that you didn't include into the example?
Your dump contains an array reference within an array reference. You need to elaborate on what the expected structure of TestCasesToRun arrayref is, and show the code which processes it in the test runner.
As per your last comment:
Change your JavaScript code to join using simple comma: checkedValue += ",";
Change your Perl assignment to: our #testCasesToRun = split(/,/, $var->param("tcs"));

Resources