.getOrCreate()

Returns the first entry that matches the provided filter. If no entry is found, creates and pushes a new one with the provided data into the collection.

getOrCreate(filter, data)

Parameter

Description

filter

Callback

Function to test each element of the array. Return a value that coerces to true to keep the element, or to false otherwise

data

Data

Entry's data

Returns

Description

First entry that matches the filter or, if no entry is found, the newly created one

// Note: Users is a collection.

Users.create({ name: 'Richard' }); // { name: 'Richard' }

Users.getAll(); // [{ name: 'Richard' }]
Users.get(u => u.name === 'Henry'); // null

Users.getOrCreate(u => u.name === 'Henry', { name: 'Henry' }); // { name: 'Henry' }

Users.getAll(); // [{ name: 'Richard' }, { name: 'Henry' }]

Last updated