CasperJS with JSON.parse - casperjs

When I run the following code, I get the following:
TypeError: 'undefined' is not a function (evaluating 'this.emit('error', error)')
I asked this earlier, but im rephrasing. It appears that the JSON object is undefined in the casper function.
If I do JSON.parse() outside, then its not undefined.
Thoughts on how to get this working?
var casper = require("casper").create({
verbose: true,
logLevel: 'debug',
});
var site = 'http://my.internalsite.com';
casper.start(site);
casper.run(function() {
var currentURL = this.getCurrentUrl();
this.echo('URL: ' + currentURL);
var json_string = JSON.parse(this.getPageContent());
this.echo(json_string);
this.exit();
});

This could possibly be due to this.exit() getting called before JSON.parse(this.getPageContent())
You could try the following:
var casper = require("casper").create({
verbose: true,
logLevel: 'debug',
});
var site = 'http://xkcd.com/info.0.json';
casper.start(site);
casper.then(function() {
var currentURL = this.getCurrentUrl();
this.echo('URL: ' + currentURL);
var json_string = JSON.parse(this.getPageContent());
require('utils').dump(json_string);
});
casper.run();

Related

dateObj.getMonth() not working in nativescript

I am trying to get the month from the date object . But I am getiing error whci displays says "getMonth is not a function".
Since I saw some Java script tutorials , where getMonth() is supported in ES6.
Am I missing importing anything.
var config = require("../../shared/config");
var fetchModule = require("fetch");
var ObservableArray = require("data/observable-array").ObservableArray;
var datePickerModule = require("tns-core-modules/ui/date-picker");
function TaskListViewModel(items){
var listModel = new ObservableArray(items);
listModel.getTaskList = function()
{
return fetchModule.fetch(config.TaskListURL,{
method:"Get"
})
.then(handleErrors)
.then(function(response){
return response.json();
}).then(function(data) {
data.forEach(function(element) {
var dateObj = Date.parse(element.followUpDate)
listModel.push({
fallowUpDateMonth:dateObj.getMonth(),
faloowUpDateDay:dateObj.getDate(),
fallowupDateYear:dateObj.getFullYear(),
});
});
});
};
return listModel;
}
module.exports = TaskListViewModel;

mocha chaijs : $ is not defined

I'm trying to setting up automated test.
I'm using zombiejs for the browser, mochaJs for the test framework and chaiJs for assertion. I want to use chai-jq for jquery assertion:
var expect = require('chai').expect
var chai = require("chai");
var chaiJq = require('chai-jq');
Browser = require('zombie'),
browser = new Browser();
chai.use(chaiJq);
describe('Test', function(){
before(function(done) {
browser.visit('http://localhost/login.php', done);
});
describe('Connexion au site', function() {
before(function(done) {
browser
.fill('login', 'foo')
.fill('password', 'bar')
.pressButton('Connexion', done);
});
it('should be successful (code 200)', function() {
browser.assert.success(200);
});
});
describe('', function() {
browser.visit('http://localhost/activites/nationales/accueil.php');
it('contain text', function() {
var $elem = $("<div id=\"hi\" foo=\"bar time\" />");
expect($elem)
// Assertion object is `$elem`
.to.have.$attr("id", "hi").and
// Assertion object is still `$elem`
.to.contain.$attr("foo", "bar");
});
});
When I run the test i've got the error ReferenceError: $ is not defined

Nativescript - Pass array from home-view-model to home.js

I´m having a hard time understanding how to perform this action(as the title says), and maybe someone could help me understand the process, my code is below:
My home-view-model:
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;
var http = require("http");
function createViewModel() {
http.getJSON("http://myJsonfile").then(function (r) {
var arrNoticias = new ObservableArray(r.data);
return arrNoticias;
}, function (e) {
});
}
exports.createViewModel = createViewModel;
I have done a console.log of the arrNoticias before i have putted it inside a callback function and it returns [object object] etc...and then i have done this:
console.log(arrNoticias.getItem(0).titulo);
and it returns the info i need!.
Then in my home.js file i have this:
var observableModule = require("data/observable")
var ObservableArray = require("data/observable-array").ObservableArray;
var arrNoticias = require('./home-view-model.js');
console.log(arrNoticias.getItem(0).titulo);
and the result in the console is:
TypeError: arrNoticias.getItem is not a function. (In 'arrNoticias.getItem(0)', 'arrNoticias.getItem' is undefined)
My question is, how does this action is perform? passing the data from view-model to the .js file?
Thanks for your time
Regards
As that function send a URL request so probably it's an async function, which is on hold while requesting so that's why you get undefined. Normally, you will want your function that sends a URL request to return a promise. Based on that promise, you will the result as expected after the request is done. So:
function createViewModel() {
return new Promise<>((resolve, reject) => {
http.getJSON("http://myJsonfile").then(function (r) {
var arrNoticias = new ObservableArray(r.data);
resolve(arrNoticias);
}, function(e) {
reject(e);
});
}), (e) => {
console.log(e);
})
}
In home.js:
var homeVM= require('./home-view-model.js');
var arrNoticias;
homeVM.createViewModel().then(function(r) {
arrNoticias = r;
});

Parse Response Variable Issues with HTTP Request and Cloud Code

I am trying to run an HTTP Request in an afterSave. It executes correctly, but im getting errors with the response variable I was told to put in the end.
Here is the code
Parse.Cloud.afterSave("ArtPiece", function(request) {
var artistURL = 'http://www.gallery-admin-dev.iartview.com/Gilmans/imageSave.php';
var artPiece = request.object;
if (typeof request.image === "undefined") {
var pieceName = artPiece.get("piece_name");
var url = artPiece.get("imageURL");
Parse.Cloud.httpRequest({
url: artistURL,
params: {
pieceName : pieceName,
url : url
},
success: function(httpResponse) {
response.success();
},
error: function(httpResponse) {
response.error('Request failed with response code ' + httpResponse.status)
}
});
}
});
The problem is the success and error functions.
Things I have tried that have not worked:
Putting response as a second parameter in the afterSave Call (it says can not call .success of an undefined variable)
Getting rid of the response lines of code (Result: success/error was not called)
Calling Response as its own function response('Request failed with response code ' + httpResponse.status) it just says response is undefined.
I dont really care about having any response or not, I just want the code to work properly.
How is this 'response' variable supposed to be set up?
Thank you!
There was an extra url in the parameters, a reference to a probably undefined artistURL...
Parse.Cloud.afterSave("ArtPiece", function(request) {
var artistURL = 'http://www.gallery-admin-dev.iartview.com/Gilmans/imageSave.php';
var artPiece = request.object;
if (typeof request.image === "undefined") {
var pieceName = artPiece.get("piece_name");
var url = artPiece.get("imageURL");
var params = { pieceName : pieceName };
return Parse.Cloud.httpRequest({ url: artistURL, params: params });
}
});

How to make wrapped jQuery promise fire the reject callback on error?

I'm wrapping a simple jQuery promise with RSVP and noticed that when I cause an error on purpose the failure callback is never invoked. I assume it's because when you use vanilla jQuery and the callback throws an error, the returned promise will not be moved to failed state (the opposite of the spec).
If I need to use jQuery $.ajax but I want to get true resolve/reject callbacks with RSVP what (if anything) can I do to the example below?
var peoplePromise = new Ember.RSVP.Promise(function(resolve, reject) {
$.getJSON('/api/people/', resolve).fail(reject).error(reject);
});
var catPromise = new Ember.RSVP.Promise(function(resolve, reject) {
$.getJSON('/api/cats/', resolve).fail(reject).error(reject);
});
Ember.RSVP.all([peoplePromise, catPromise]).then(function(things) {
things[0].forEach(function(hash) {
var thing = App.Person.create(hash);
Ember.run(self.people, self.people.pushObject, thing);
});
things[1].forEach(function(hash) {
var wat = hash.toJSON(); //this blows up
var thing = App.Person.create(hash);
Ember.run(self.people, self.people.pushObject, thing);
});
}, function(value) {
alert(value.status + ": promise failed " + value.responseText);
});
Example here: http://www.youtube.com/watch?feature=player_detailpage&v=g5CSaK3HqVA#t=1080
var ajaxPromise = function(url, options){
return Ember.RSVP.Promise(function(resolve, reject) {
var options = options || {};
options.success = function(data){
resolve(data);
};
options.error = function(jqXHR, status, error){
reject([jqXHR, status, error]);
};
$.ajax(url, options);
});
};
var peoplePromise = ajaxPromise('/api/people/',{
dataType: "json"
});
var catPromise = ajaxPromise('/api/cats/',{
dataType: "json"
});
Ember.RSVP.all([peoplePromise, catPromise]).then(function(things) {
things[0].forEach(function(hash) {
var thing = App.Person.create(hash);
Ember.run(self.people, self.people.pushObject, thing);
});
things[1].forEach(function(hash) {
var wat = hash.toJSON(); //this blows up
var thing = App.Person.create(hash);
Ember.run(self.people, self.people.pushObject, thing);
});
}, function(args) {
var jqXHR = args[0];
alert(jqXHR.status + ": promise failed " + jqXHR.responseText);
});
http://emberjs.jsbin.com/aREDaJa/1/

Resources