Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

RemoveAll example not working #79

Closed
rnenjoy opened this issue Jun 11, 2016 · 5 comments
Closed

RemoveAll example not working #79

rnenjoy opened this issue Jun 11, 2016 · 5 comments
Assignees

Comments

@rnenjoy
Copy link

rnenjoy commented Jun 11, 2016

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
Copy link
Contributor

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
Copy link
Member

@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
Copy link
Contributor

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
Copy link
Member

Ah right, makes sense. So what you wrote here #79 (comment) is probably what we should put into the docs?

@deontologician
Copy link
Contributor

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

@chipotle chipotle self-assigned this Jun 14, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants