Summary
When adding a new smart relationship, the relationship is added successfully in the collection table view but when clicking into a collection item’s summary/detail view an error is thrown on the Admin Backend:
[forest] 🌳🌳🌳 Unexpected error: Cast to ObjectId failed for value "{
country: 'US',
street: 'fake',
complement: '',
state: 'fake',
city: 'fake',
zipCode: 'fake'
}" (type Object) at path "_id" for model "Company"
Feature(s) impacted
- Smart relationships
- Viewing collection item summary/details
Observed behavior
A smart relationship:
{
field: 'balance account',
type: 'String',
reference: 'CompanyAccount._id',
get: async company => {
const accounts = await companyAccountModel.aggregate([
{
$lookup: {
from: 'users',
localField: 'companyId',
foreignField: '_id',
as: 'user_doc',
},
},
{
$match: {
'user_doc._id': company._id,
},
},
]);
return accounts?.[0]?._id;
},
},
on a collection named “Companies” results in a successful linking to the “CompanyAccount” collection in the collection table view (when it exists):
but when I click on an item in this collection to view it’s summary/detail view, I get an error:
(see the failure logs section for the stack trace)
This only occurs for “Companies” that have an “account” attribute. For “Companies” that haven’t gone through the onboarding process, there is no “account” attribute and the summary/details view displays without error.
Expected behavior
I would expect to be able to click into a “Companies” collection item to view its summary/details view. The smart relationship field “balance account” should be available in the summary/details view.
Failure Logs
Stack trace:
[forest] 🌳🌳🌳 Unexpected error: Cast to ObjectId failed for value "{
country: 'US',
street: 'fake',
complement: '',
state: 'fake',
city: 'fake',
zipCode: 'fake'
}" (type Object) at path "_id" for model "Company"
{
"stringValue": "\"{\n country: 'US',\n street: 'fake',\n complement: '',\n state: 'fake',\n city: 'fake',\n zipCode: 'fake'\n}\"",
"kind": "ObjectId",
"value": {
"country": "US",
"street": "fake",
"complement": "",
"state": "fake",
"city": "fake",
"zipCode": "fake"
},
"path": "_id",
"reason": {},
"valueType": "Object",
"stack": "CastError: Cast to ObjectId failed for value \"{\n country: 'US',\n street: 'fake',\n complement: '',\n state: 'fake',\n city: 'fake',\n zipCode: 'fake'\n}\" (type Object) at path \"_id\" for model \"Company\"\n
Context
- Project name: subscribe.so
- Team name: Operations
- Environment name: DEVELOPMENT | SUBSCRIBEADMIN (FEATURE/SUB-13)
- Agent type & version: forest-express-mongoose@^8.7.0
Recent changes made:
- Added a smart relationship to the “Companies” collection (see the Observed Behavior section) - this error does not occur when this smart relationship is removed
FYI on the “Companies” collection:
The “Companies” collection inherits it’s schema from the “Users” collection via discriminators so a company is a user with a userType
attribute of “company” (userType
is the discriminatorKey).
Companies have an “account” attribute that has multiple levels of nested schemas:
const addressSchema: Schema = new Schema(
{
street: {
type: String,
},
complement: {
type: String,
},
city: {
type: String,
},
state: {
type: String,
},
zipCode: {
type: String,
},
country: {
type: String,
default: 'US',
},
},
{ _id: false, id: false },
);
const companyOwnerSchema: Schema = new Schema(
{
name: {
type: String,
},
title: {
type: String,
},
phone: {
type: String,
},
email: {
type: String,
},
},
{ _id: false, id: false },
);
const accountCompanySchema: Schema = new Schema(
{
legalName: {
type: String,
},
corporationType: {
type: String,
},
stateOfIncorporation: {
type: String,
},
owner: companyOwnerSchema,
address: addressSchema,
},
{ _id: false, id: false },
);
const companySchema: Schema = new Schema(
{
account: {
type: accountCompanySchema,
},
invoiceSettings: invoiceSettingsSchema,
},
{ _id: false },
);
// userModel not included here for brevity
const companyModel = userModel.discriminator<Company & Document>('Company', companySchema, PermissionEnum.company);
Ideas
The cast error makes it seem like something is trying to use company.account.address as the input to an ObjectId() call. I’m wondering if there is some unexpected naming conflict that Forest is running into with that account attribute? The naming of CompanyAccount and balance account are obviously similar but not exactly the same so this is really stumping me.
Thanks for any help.