rethinkdb / horizon-docs

Other
24 stars 36 forks source link

RemoveAll example not working #79

Closed rnenjoy closed 8 years ago

rnenjoy commented 8 years ago

Hello,

Maybe im doing this the wrong way, but i tried the example:

const hz = Horizon();
const messages = hz("messages");

// get all messages from Bob and Agatha...
var messageList = messages.findAll({from: "bob"}, {from: "agatha"}).fetch();

// ...and delete them
messages.removeAll(messageList);

I couldn't get this working since removeAll expects an object, and messageList seems to be just a snapshot? This is how I made it, but then again, i'm a n00b:

const hz = Horizon();
const messages = hz("messages");

// get all messages from Bob and Agatha...
messages.findAll({from: "bob"}, {from: "agatha"}).fetch().subscribe(messageList => {
     // ...and delete them
     messages.removeAll(messageList);
});
deontologician commented 8 years ago

Your second example is right. Alternately, if you want to check the responses of the removeAll:

messages.findAll({ from: 'bob' }, {from: 'agatha' }).fetch()
  .map(messageList => messages.removeAll(messageList))
  .subscribe({
    next(removedId) { console.log(`id ${removedId} was removed`) },
    error(err) { console.error(`Error removing items! ${err}`) },
    complete() { console.log('All items removed successfully') },
  })
danielmewes commented 8 years ago

@deontologician Would it fix the example at http://horizon.io/api/collection/#removeall to add a .toArray() here?

var messageList = messages.findAll({from: "bob"}, {from: "agatha"}).fetch().toArray();
deontologician commented 8 years ago

No, .toArray() still returns an observable, instead of getting [{id:1}, ...] you'd get [[{id:1}, ...]] since it's already emitting an array. In this case there's no way around a callback like @rnenjoy does with .subscribe, or by chaining operations like in my example. It's fundamentally an asynchronous operation, and the example is written in a style that requires the results to be available synchronously

danielmewes commented 8 years ago

Ah right, makes sense. So what you wrote here https://github.com/rethinkdb/horizon-docs/issues/79#issuecomment-225373482 is probably what we should put into the docs?

deontologician commented 8 years ago

Yeah, it's effectively the same as @rnenjoy's example, but is more general since it shows how to get the removed ids back out