Parse React - Observing objects created by Parse.User.current ()

I'm using the excellent parse-react to get Parse and ReactJS working together (nb, I've only been playing around for hours so sorry if I misunderstood any basics of react). Everything was going well until I queried the table for all objects created by the current user (Parse.user.current ())

The observation method works correctly when loaded and the view is rendered with the correct objects (objects created by the current user). However, if I mutate the data and add a new object, then the view is not re-rendered.

Abstract code:

module.exports = React.createClass({
    mixins: [ParseReact.Mixin],
    getInitialState: function() {
        return {
            selected: null
        };
    },
    observe: function() {
        return {
            places: (new Parse.Query('Place'))
                        .equalTo('user', Parse.User.current())
                        .descending('createdAt')
        };
    },
    clickHandler: function(event) {

        var id = event.target.id;
        if (id === 'new') {
            ParseReact.Mutation.Create('Place', {
                name: 'New Place',
                user: Parse.User.current()
            }).dispatch();
        } else if(id.indexOf('Place:') === 0) {
            this.setState({
                selected: id.substring(6)
            });
        }

    },
    render: function() {

        var that = this;
        var navItems = this.data.places.map(function(place) {
            return (
                <UserNavItem id={place.id} key={place.id} label={place.name} selected={that.state.selected === place.objectId} onClick={that.clickHandler}/>
            );
        });

        return (
            <ul>
                {navItems}
                <UserNavItem id='new' label='+ New Place' onClick={this.clickHandler} />
            </ul>
        );

    }
});

      

If I remove the part of the request that specifies the user:

.equalTo('user', Parse.User.current())

      

Then it works; objects of the new location appear in the list when added.

Anyone have any idea what I am doing wrong?

Am I using Parse queries incorrectly? It seems odd that getting data pertaining to the current user is a bit painful when it seems such a common use case?

Thank!

+3


source to share


1 answer


The solution is to call the .refreshQueries()

bean method when the new object is successfully created in Parse, as described here .

My updated example:



            ParseReact.Mutation.Create('Place', {
                name: 'New Place',
                user: Parse.User.current()
            })
            .dispatch()
            .then(function() {
                this.refreshQueries();
            }.bind(this));

      

Many thanks to Joshua Sierles on the ParseReact giseub repository for pointing out the solution. If you are on SO Joshua I will give you credit if you post your answer here: D

+3


source







All Articles