Parse TypeError无法读取属性'条目'在main.js的未定义时间为39:27

时间:2016-01-01 20:20:14

标签: javascript parse-platform cloud-code

我有一个快速的项目,我想要使用推送通知。我尝试使用带有.js文件的作业计划来使用解析服务器。问题是,当我在作业状态窗口上运行作业时,我收到此错误:

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

这是我的main.js文件:

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");
    }
});
  });

我该如何避免这个问题?

2 个答案:

答案 0 :(得分:1)

我使用Fiddler来显示xml并找不到feed节点,应该是res.rss.channel

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

enter image description here

答案 1 :(得分:0)

响应数据res.feed未定义。你可以通过console.log(res)找出问题所在。还要确保签名对于xmlreader.read(httpResponse.text,function(err,res))是正确的,不确定为什么err是函数的第一个参数,很可能res是第一个。

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");
                }
            }
        });
    });

}