1

I have these two classes in parse

This is my TestItem class and it has items that are available. Then I have my UserFavorites Class:

enter image description here

It has Items that user have favorited. As you can see Buffalo Waffle Fries and Shrimp Po Boy are in both classes. In UserFavs I have a pointer from the item in UserFav to that item in TestItem so I can match them for a purpose not relevant to this question. My problem is that the items in the class TestItem get deleted and new ones are added daily. So the pointer in testItem is no longer valid. So I can no longer match them. As you can see I manually updated the pointer for Shrimp Po Boy, just to show this. I need some help with my javascript code, that will match the items in TestItem to ones in UserFav. And then update the pointer in UserFav to match the objectId in TestItem class. I hope that makes sense if not ask me to clarify. Also it could be possible that more than one user has favorited the same item so I would need it to update both pointers to TestItem class.

Hers what I am working with so far:

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

var TestItem = Parse.Object.extend("TestItem");
var UserFav = Parse.Object.extend("UserFavourite");
var testItemsQuery = new Parse.Query(TestItem);
var userFavoritesQuery = new Parse.Query(UserFav);

testItemsQuery.find().then(function(testItemRes) {
    for (var i = 0; i < testItemRes.lenght; i++) {
        var item = testItemRes.get('item');
        userFavoritesQuery.equalTo('item', item);
        userFavoritesQuery.then(function(userFavoritesRes) {
            for (var i2 = 0; i2 < userFavoritesRes.length; i2++) {
                //update the testItem pointer here
                //but this doesnt work, I think its because the query is async
                //But I am using .then, so maybe I need some promises?  
            }
        });
    }
});
});

Thanks so much for the help in advance!

2 Answers 2

1

A couple of things:

  • You have to create new query instances for each iteration. Since this is all happening async, trying to reuse the same query object, with resetting the "item", will surely lead to problems.
  • A query object is not itself "then-able". Perhaps you forgot to write "find()" in there?
  • Update the userFavorite's testItem field with the the correct object
  • After updating an item, push it to an array so you can use saveAll

Code example:

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

    var TestItem = Parse.Object.extend("TestItem");
    var UserFav = Parse.Object.extend("UserFavourite");
    var testItemsQuery = new Parse.Query(TestItem);

    var userFavoriteObjectsToSave = [];

    testItemsQuery.each(function(testItem) {
        var item = testItem.get('item');
        var userFavoritesQuery = new Parse.Query(UserFav);
        userFavoritesQuery.equalTo('item', item);
        return userFavoritesQuery.each(function(userFavorite) {
            userFavorite.set('testItem', testItemRes);
            userFavoriteObjectsToSave.push(userFavorite);
        });
    }).then(function () {
        Parse.Object.saveAll(userFavoriteObjectsToSave);
    });
});
Sign up to request clarification or add additional context in comments.

6 Comments

Basically, as simply as I can put it. My TestItems class get deleted and updated, which makes the pointer to TestItem class in UserFavourites Class wrong. So I want my script to find all the items in TestItems class that match items in UserFavourites class, that match. Then update the pointer in the UserFavourites class for those items to point to the correct item in TestItem class. Does that make more sense?
How do you intend to find matches if the pointers are not valid? Do you want to match on the item String field?
Ah. I think I understand now. I have updated the answer with a solution that I think should work for you.
this doesn't work because the queries are async the code keeps running and the array doesn't get filled, jot stays blank because the query doesn't return the info fast enough. So I think it needs some promises
Ah, sorry, I'm updating it now.
|
1

This should do the trick

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

var TestItem = Parse.Object.extend("TestItem");
var UserFav = Parse.Object.extend("UserFavourite");
var testItemsQuery = new Parse.Query(TestItem);

var userFavoriteObjectsToSave = [];


}).then(function () {
    Parse.Object.saveAll(Save);
});

});

I just tweaked what I use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.