-1

I have a swift project in which i want use push notifications. I tried use parse server using a job schedule with .js files. The problem is that when i run the job on job status window i get this error:

 "TypeError cannot read property 'entry' of undefined at main.js at 39:27"

Here is my main.js file:

var xmlreader = require('cloud/xmlreader.js');

var url = "http://www.ilsecoloxix.it/homepage/rss/homepage.xml";

function SavePost(title, link){
var PostClass = Parse.Object.extend("Post");
var post = new PostClass();
post.set("title", title);
post.set("link", link);
post.save();
}

function SendPush(title, link){
var query = new Parse.Query(Parse.Installation);
Parse.Push.send({
    where: query,
    data: {
        url: link,
        alert: title,
        sound: "default"
        }
    }, {
        success: function() {
            SavePost(title, link);
            response.success("Push sent to everyone");
        },
        error: function(error) {
            response.error("Error sending push: "+error);
        }
    });
}

Parse.Cloud.job("fetchPosts", function(request, response) {
Parse.Cloud.httpRequest({
    url: url,
    success: function(httpResponse) {
        xmlreader.read(httpResponse.text, function (err, res){
            var newPost = res.feed.entry.at(0);
            var title = newPost.title.text();
            var link = "";
            newPost.link.each(function (i, linkObj){
                if (linkObj.attributes().rel == "alternate"){
                    link = linkObj.attributes().href;
                }
            });

            var PostClass = Parse.Object.extend("Post");
            var query = new Parse.Query(PostClass);
            query.equalTo("link", link);
            query.find({
                success: function(results) {
                    console.log(results);
                    if (results.length == 0){
                        SendPush(title, link);
                    } else {
                        response.error("Post already pushed");
                    }
                }
            });
        });

       },
    error: function(httpResponse) {
        console.error('Request failed with response code ' + httpResponse.status);
        response.error("Error fetching posts from feed");
    }
});
  });

How can i avoid this problem?

luk2302
  • 55,258
  • 23
  • 97
  • 137
Swift1
  • 349
  • 1
  • 4
  • 22
  • 1
    have you tried debugging it, apparently `feed` does not have a member `entry` – luk2302 Jan 01 '16 at 20:22
  • if i use the code of Tony Bao i can go next, but give me another error:cannot read property 'title' of undefined at main.js 43:24. Why? – Swift1 Jan 01 '16 at 21:31
  • Because you need to debug it. Apparently you are trying to get the title of an undefined variable. – luk2302 Jan 01 '16 at 21:33
  • how can i debug it? now i get this error: Reference Error: response is not defined at e.sendpush.parse.push.send.success – Swift1 Jan 01 '16 at 21:37

2 Answers2

1

I am using Fiddler to display xml and find no feed node, should be res.rss.channel

http://www.ilsecoloxix.it/homepage/rss/homepage.xml

enter image description here

Tony Bao
  • 922
  • 2
  • 12
  • 23
  • Thank you...i changed my code with res.rss.channel as in your advice and now gives me that error: "post already pushed" but i didn't received any push notification... – Swift1 Jan 02 '16 at 22:33
  • 1
    This is not error, just find some posts, or if (results.length == 0) is wrong, you need to find if results.length is undefined. – Tony Bao Jan 02 '16 at 23:05
  • In the console log i get this message: Ran job fetchPosts with: Input: {} Result: Post already pushed. input seems to be a blank string...why? – Swift1 Jan 02 '16 at 23:28
  • it seems like results.length is != 0... pheraps is a problem in sax.js file? here is the file https://raw.githubusercontent.com/isaacs/sax-js/master/lib/sax.js please help me... – Swift1 Jan 03 '16 at 10:26
0

Response data res.feed is undefined. You can console.log(res) to figure out what is wrong. Also make sure the signature is correct for xmlreader.read(httpResponse.text, function (err, res), not sure why err is the first parameter of the function, most likely res is the first.

if(res && res.feed && res.feed.entry){
   var newPost = res.feed.entry.at(0);
   var title = newPost.title.text();
        var link = "";
        newPost.link.each(function (i, linkObj){
            if (linkObj.attributes().rel == "alternate"){
                link = linkObj.attributes().href;
            }
        });

        var PostClass = Parse.Object.extend("Post");
        var query = new Parse.Query(PostClass);
        query.equalTo("link", link);
        query.find({
            success: function(results) {
                console.log(results);
                if (results.length == 0){
                    SendPush(title, link);
                } else {
                    response.error("Post already pushed");
                }
            }
        });
    });

}
Tony Bao
  • 922
  • 2
  • 12
  • 23
  • i tried your code and now gives me that error: cannot read property 'title' of undefined at main.js 43:24. Why? – Swift1 Jan 01 '16 at 21:00
  • Sorry for late response, because my code is not complete. I will update it. I think you need to debug the code to find why res.feed is undefined. Otherwise this code has no meaning. You can add console.log(res); to find all the properties of res and figure out why feed is not there, wrong property name – Tony Bao Jan 02 '16 at 01:02
  • Thank you for the interest in my problem. Now with your code updated i get this error when parse deploy:Uncaught SyntaxError: Unexpected token } in main.js:64 ...i tried to cancel } but i get the same error. – Swift1 Jan 02 '16 at 15:16
  • i notice one thing: if i change my var url (using my original main.js) to http://www.ilsole24ore.com/rss/primapagina.xml i get this error instead: error fetching post from feed... Pheraphs then it's a problem with the xml file? – Swift1 Jan 02 '16 at 15:45