简体   繁体   中英

Deleting object from array in Parse Cloud Code (Javascript)

Below is my JS code that is supposed to delete a pointer reference to a 'Game' object which is stored in an array under the key 'games' in the 'team' object. Fetching works and all the 'success' logs below get called meaning that the .remove and .save functions get called, but for some reason, this does not do anything and the pointers still exist in the arrays after the execution of this code. What am I doing wrong?

var queryTeam = request.object.get("awayTeamID");

queryTeam.fetch({
    success: function(awayTeam){

        console.log("Success in Fetching awayTeam Object. Destroying Now.");

        awayTeam.remove(awayTeam.get("games"), request.object);
        awayTeam.save();

        var queryTeam = request.object.get("homeTeamID");

        queryTeam.fetch({
            success: function(homeTeam){

                console.log("Success in Fetching homeTeam Object. Destroying Now.");


                homeTeam.remove(homeTeam.get("games"), request.object);
                homeTeam.save();
            },
            error: function(homeTeam, error){
                console.error("Error removing game from homeTeam array! " + error.code + ": " + error.message);
            }
        });
    },
    error: function(myObject, error){
        console.error("Error removing game from awayTeam array! " + error.code + ": " + error.message);
    }

});

您的删除方法不正确,您不需要获取数组即可将其删除,应该在对象上执行删除请求:

awayTeam.remove("games", request.object);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM