How to extract individual object from an ajax callback - ajax

I am getting back the following from an AJAX call which exactly matches the syntax in my manual:
var data = {
coach100: {
PID: '23169',
POrt: '11'
},
coach200: {
PID: '23170',
POrt: '11'
}
};
Now I want to extract one piece of data. The manual syntax is:
data.coach100.PID
but the Firebug console says: "TypeError: data.coach100 is undefined"
How to format the data in the variable "data" so that it can be extracted using dot syntax?

This runs just fine in JSfiddle - http://jsfiddle.net/GWFe9/
My guess is that you're defining and calling data in the wrong place.
If you're calling your data outside of the success of the ajax but also defining it there you're going to have a bad time.
try adding var data; to the top of your doc so its a global variable - then when you get the success from ajax define your data like this -
data = {
coach100: {
PID: '23169',
POrt: '11'
},
coach200: {
PID: '23170',
POrt: '11'
}
};
then you can call it at any time after it's been loaded!

Related

Apollo client ignoring argument for GQL REST query

I have a query that is defined like this:
export const getProjectCosts = gql`
query GetProjectCosts($projectId: Int) {
ProjectCostList #rest(type: "ProjectCostList", path: "ProjectCosts/{args.projectId}") {
id
projectId
cost
description
costType
budgetYear
createdByUser
createdDate
}
}
`;
export const useGetProjectCostsListQuery = (baseOptions?: QueryHookOptions<ProjectCostList>) => {
const options = { ...baseOptions };
return useQuery<ProjectCostList>(getProjectCosts, options);
};
I call it like this:
const {
loading: projectCostLoading,
error: projectCostError,
data: projectCostData,
} = useGetProjectCostsListQuery({
variables: {
projectId: args.defaultValues.id,
},
});
I have verified in the debugger that the argument is sent correctly.
However, in the browser I see a warning:
Warning: RestLink caught an error while unpacking ProjectCosts/{args.projectId}|args.projectId This tends to happen if you forgot to pass a parameter needed for creating an #rest(path, or if RestLink was configured to deeply unpack a path parameter that wasn't provided. This message will only log once per detected instance. Trouble-shooting hint: check #rest(path: and the variables provided to this query.
(anonymous) # restLink.ts:567
And a request is sent to api/ProjectCosts/ instead of api/ProjectCosts/1234, which not surprisingly fails with a HTTP 404 error.
Is this a bug or am I doing something wrong?
I found this issue on Github, which looks very similar to what I'm experiencing.
We're using a custom .NET backend.
If it's a bug, is there a workaround?
The problem was that the parameter has to be declared twice.
This is the solution:
export const getProjectCosts = gql`
query GetProjectCosts($projectId: Int!) {
ProjectCostList(projectId: $projectId) #rest(type: "ProjectCostList", path: "ProjectCosts/{args.projectId}") {

In my Cypress.io tests why do I need to treat a cy.task like it is asyncronous when its not

I have Cypress.io tests that use a simple Cypress task to log some table information to the terminal. For example I have a test like so:
it("Writes some console logs and checks a val", () => {
cy.task("rowLog", { id: 1, name: "foo", type: "bar" });
let one = 1;
expect(one).to.equal(2);
});
And the task, "rowLog" like so:
module.exports = (on, config) => {
on("task", {
rowLog(data) {
// use node.js console.table to pretty print table data:
console.table(data);
return null;
},
}
But the result of rowLog will not display in my terminal if I run Cypress headlessly via Cypress run. This is because the test will fail. If we switch the test so that it passes, then it will show.
However I just realized that if I treat rowLog like it's async like below. It will print the results to the terminal:
it("Writes some console logs and checks a val", () => {
// add a .then to task:
cy.task("rowLog", { id: 1, name: "foo", type: "bar" }).then(() => {
let one = 1;
expect(one).to.equal(2);
});
});
This is not what I would expect from the docs. They say that:
cy.task() yields the value returned or resolved by the task event in the pluginsFile.
(source)
And that a task can yield either a promise or a value.
I'm new to Cypress here-- is there something I'm missing or doing wrong? I'd like to be able to not have to chain my tasks with .then statements if it is just synchronous stuff like writing output to ensure everything is emitted to my terminal.
If you look into the type definition of cy.task command, you will see that it returns a Chainable (that is a promise-like entity). So it behaves like any other cy command (ansynchrounously).
As for the yield either a promise or a **value** - this statement refers to the handler of the task, not the task itself. As for the other command, Cypress will wrap a returned value into a promise if it was not done by the handler.

What could cause a HPE_INVALID_METHOD error using fastify and nunjucks?

I disabled all plugins on the server except point-of-view.
fastify.register(require('point-of-view'), {
engine: {
nunjucks: require('nunjucks')
},
templates: 'server/views',
includeViewExtension: true
})
The handler is calling the view function.
reply.view('/v1/main', {
nonce: nanoid(1),
token: nanoid(1)
})
The complete error message is:
{"level":50,"time":1547208496144,"msg":"client error","pid":16013,"hostname":"jer-ryzentwo","err":{"type":"Error","message":"Parse Error","stack":"Error: Parse Error","bytesParsed":0,"code":"HPE_INVALID_METHOD","rawPacket":{"type":"Buffer","data":[22,3,1,2,0,1,0,1,105,3,3,167,247,206,59,236,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},"v":1}
{"level":50,"time":1547208496145,"msg":"client error","pid":16013,"hostname":"jer-ryzentwo","err":{"type":"Error","message":"Parse Error","stack":"Error: Parse Error","bytesParsed":0,"code":"HPE_INVALID_METHOD","rawPacket":{"type":"Buffer","data":[10,0,1,0,2,2,20]}},"v":1}
I cannot pinpoint the source of this issue.
Any idea what and why a HPE_INVALID_METHOD error is being generated?

Nightwatch Unable to locate element using xpath

I am new to Nightwatch and trying to write a simple test. I am getting the following error.
ERROR: Unable to locate element: "//label[data-id="CC"]" using: xpath
let suite = {
"Test 1.": function (client) {
client.useXpath();
page = client.page.configure.deviceProperties.default();
page.navigate();
page.waitForLoader();
},
"column selector": function(client){
page
.verify.containsText('//label[data-id="CC"]', 'CC')
client.pause(100);
}
I also have tried with
'label[data-id="CC"]'
'label[#data-id="CC"]'
What am I doing wrong? Thanks in advance.
-dj
the attribute must contains prefix # using xpath (as you have specified)
like "//label[#data-id='CC']" which should works
and in CSS it looks like "label[data-id='CC']"
add a waiting step before test like waitForElement
[using xpath]
page.waitForElementVisible("//label[#data-id='CC']", 2000, false, function(result){
if (result.status === 0) {
page.verify.containsText("//label[#data-id='CC']", 'CC')
}
});
or expect :
page.expect.element("//label[#data-id='CC']").text.to.contain('CC').before(2000);
use ('//label[#data-id="CC"]', 'CC'), and you should try your xpath in the browser console to test whether the xpath is correct or not.

Create highstock using AJAX/JSON

I build a highstock using AngularJs,
This is the code currently look like http://jsfiddle.net/j06ivy/r88yszk0/
My question is how can I returns JSON data (http link) instead of put these data below in javascript code?
$scope.chartConfig.series.push({
id: 1,
data: [
[1147651200000, 23.15],
[1147737600000, 23.01],
[1147824000000, 22.73],
[1147910400000, 22.83],
[1147996800000, 22.56],
[1148256000000, 22.88],
[1148342400000, 22.79],
[1148428800000, 23.50],
[1148515200000, 23.74],
[1148601600000, 23.72],
[1148947200000, 23.15],
[1149033600000, 22.65]
]
},   {
id: 2,
data: [
[1147651200000, 25.15],
[1147737600000, 25.01],
[1147824000000, 25.73],
[1147910400000, 25.83],
[1147996800000, 25.56],
[1148256000000, 25.88],
[1148342400000, 25.79],
[1148428800000, 25.50],
[1148515200000, 26.74],
[1148601600000, 26.72],
[1148947200000, 26.15],
[1149033600000, 26.65]
]
}
);
I try to build on my webserver
http://52.74.94.173/ivy-demo-project/highstock-json.html
I think if something wrong in here?
$scope.chartConfig.series.push({
data: jsonData
});
you can use the $http service, here are the docs: $http
place your data in a json file, then you can access you file locally like so
$http.get("../relativePathTolocalJSON/mydata.json");
your file needs to be in a legal JSON format
Your fiddle doesn't work because of several js errors - at least $http is not injected, missing parenthesis and your json file is not available from a domain other than yours.
Check this one and make sure that your json file is available and contains an array with two data series for the chart.
$http.get('http://j06ivy.tw/public/chart-data.json').success(function (jsonData) {
$scope.chartConfig.series.push({
data: jsonData
});
});

Resources