2017-11-28 8 views
0

클라우드 코드로 다양한 변형을 시도했지만 제대로 작동하지 않습니다. 기본적으로 내가 푸시 알림 기능이있어,이 기능에 나는 PFUser의 배열에 객체를 추가 할,하지만 당신은 엑스 코드에서 마스터 키를 사용할 수 없습니다 그래서 여기에 내가 가진 무엇 :Parse Cloud Code - 배열에 추가

Parse.Cloud.define("iOSPush", function (request, response) { 

console.log("Inside iOSPush"); 

var data   = request.params.data; 
var not_class  = request.params.not_class; 
var not_objectid = request.params.not_objectid; 
var not_date  = request.params.not_date; 
var userid   = request.params.userid; 
var recipientUser = new Parse.Query(Parse.User); 
recipientUser.equalTo("objectId", userid); 

// set installation query: 

var pushQuery  = new Parse.Query(Parse.Installation); 
pushQuery.equalTo('deviceType', 'ios'); 
pushQuery.matchesQuery('user', recipientUser); 
pushQuery.find({ useMasterKey: true }).then(function(object) { 
    response.success(object); 
    console.log("pushQuery got " + object.length); 
}, function(error) { 
    response.error(error); 
    console.error("pushQuery find failed. error = " + error.message); 
}); 

// send push notification query: 

Parse.Push.send({ 
    where: pushQuery, 
    data: data 
}, { useMasterKey: true }).then(function() { 

    console.log("### push sent!"); 

    // create notification: 
    var notification = { 
     "title": not_class, 
     "body": request.params.data.alert, 
     "class": not_class, 
     "objectId": not_objectid, 
     "date": not_date 
    }; 

    // get notifications: 
    var tmp_notifications = recipientUser.get("notifications"); 

    // add notification: 
    tmp_notifications.push(notification); 

    // update with notifications: 
    recipientUser.set("notifications", tmp_notifications); 
    recipientUser.save(); 

}, function(error) { 
    console.error("### push error" + error.message); 
}); 

response.success('success. end of iospush'); 

}); 

Xcode 클라우드 기능 올바른 정보를 제공했지만 함수가 끝났습니다. 함수가 어떤 이유로 알림을 설정하지 않습니다.

답변

0

나는이 게시물에 대한 답을 알아 냈습니다. 이것이 작동하지 않는 이유는 별도의 쿼리에서 사용자 개체를 먼저 가져와 마스터 키를 사용하여 저장해야하기 때문입니다. 또한 다른 하나 (parseObject.add())을 생성 할 필요없이 기존의 어레이 상에 데이터를 추가하는 기능이 있다고 발견 :

response.success('success. end of iospush'); 
: 코드 세트는 직전에 실행 된

var userQ = new Parse.Query(Parse.User); 
userQ.get(userid, { 
    success: function(theuser) { 
     console.log("### got userrrrrrrrrr!"); 
     theuser.add("notifications", n_object); 
     theuser.save(null, {useMasterKey:true}); 
    }, 
    error: function(object, error) { 
     // The object was not retrieved successfully. 
     // error is a Parse.Error with an error code and message. 
    } 
});