I have a pretty extensive site built on top of Apostrophe-CMS, heavily utilizing object schemas and the admin editor interfaces. I've been having a problem with arrays for a very long time, and I've never been able to figure out what's happening. This seems to happen with every array I define, but here's a specific example (this is an added field on apostrophe-users):
{
name: 'subscriptions',
label: 'Memberships',
type: 'array',
titleField: 'subscriptionYear',
schema: [
{
name: '_subscription',
withType: 'subscription-type',
type: 'joinByOne',
idField: 'subscriptionId',
filters: {
projection: {
type: 1,
name: 1,
description: 1,
agreement: 1,
familyMembers: 1,
title: 1,
_allowedGroup: 1,
allowedGroupId: 1,
price: 1
}
}
},
{
name: 'expirationDate',
label: 'Expiration Date',
type: 'date',
required: false
},
{
name: 'waiverSignature',
label: 'Waiver Signature',
type: 'string',
readOnly: true
},
{
name: '_payment',
withType: 'payments',
type: 'joinByOne',
idField: 'paymentId',
titleField: 'subscriptionType',
filters: {
projection: {
paymentDate: 1,
userEmail: 1,
paymentAmount: 1,
subscriptionType: 1,
subscriptionYear: 1,
receiptEmail: 1
}
}
},
{
name: 'familyMembers',
label: 'Family Members',
type: 'array',
titleField: 'familyMemberName',
schema: [
{
name: 'familyMemberName',
label: 'Family Member Name',
type: 'string'
},
{
name: 'familyMemberBirthDate',
label: 'Family Member Birth Date',
type: 'date'
}
]
}
]
}
The problem I'm running into is that deleting items from this array doesn't work unless you save the object first. If I try to delete an item from the array without saving the user first, the entry flickers in the list (disappears and almost immediately reappears). There are no console errors. But, if I open the user, save the user (even if no changes have been made), and then open the user again without closing the user pieces list, I'm able to remove items from the array and save again. Any ideas why this might be happening?
Thanks!
Is there a good place to put code to add an ID to a piece's array if it doesn't already have one?
Ultimately the problem seems to be about creating array values in code. The best place to add the ID would be when that value is first added. There's an Apostrophe utility method, self.apos.utils.generateId()
that can be used for this purpose. If you're adding the value somewhere outside of Apostrophe, the cuid
or uuid
utilities can help with that (the Apos method uses cuid).