.getOrCreate()

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

getOrCreate(filter, data)

Parameter

Description

filter

Callbackarrow-up-right

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

If this parameter is not provided, all data from the collection will be returned

data

Dataarrow-up-right

Entry's data

Returns

Description

Entries that match the filter or a single entry if the array length is equal to 1 and a filter is provided

// Note: Users is a collection.

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

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

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

Last updated