I have a shipping address database belonging to each user, they should be able to insert if no doc or update if there’s a doc. How can I go about this using autoform and simple schema?
Managed to do it in a rather hacky way
js
Template.address.onCreated(function onCreated() { Tracker.autorun(() => { Meteor.subscribe('address'); }); });
Template.address.helpers({ address() { return Address.findOne({ owner: Meteor.userId(), }); }, isAddress() { if (Address.find().count() === 0) { Session.set('isAddress', false); } else { Session.set('isAddress', true); } return Session.get('isAddress'); }, });
html
<template name="address"> {{#if isAddress}} <h2>Edit your shipping address</h2> {{> quickForm collection="Address" doc=address id="updateAddressForm" type="update"}} {{else}} <h2>Add a shipping address</h2> {{> quickForm collection="Address" id="insertAddressForm" type="insert"}} {{/if}} </template>
“insert if exists, otherwise update” is called an “upsert”. See the docs on Meteor’s Collection.upsert. Here’s a thread talking about autoform + upsert, and here’s the autoform docs on the topic
1 Like