Collaborator
The Collaborator object is a user who has access to a NocoDB base. It holds the identity of the user: the ID, name, and email address.
Overview
Collaborator objects come from these sources in NocoDB Scripts:
session.currentUserreturns a Collaborator object for the user who runs the scriptUser fieldsin records return Collaborator objectsbase.activeCollaboratorsreturns an array of Collaborator objects for the current base- Field types such as
CreatedByandLastModifiedByreturn Collaborator objects
Use this object in scripts to identify users, track ownership, assign responsibilities, and add logic for specific users.
Properties
| Property | Type | Description |
|---|---|---|
id | string | The unique identifier of the collaborator (read-only) |
name | string | null | The display name of the collaborator (read-only, may be null if not set) |
email | string | The email address of the collaborator (read-only) |
Note: All Collaborator properties are read-only. You cannot change them.
Contexts Where Collaborator Objects Appear
Session Object
The session.currentUser property returns a Collaborator object for the user who runs the script:
// Get the current user
const currentUser = session.currentUser;
output.text(`Current user: ${currentUser.name || currentUser.email}`);
output.text(`User ID: ${currentUser.id}`);
output.text(`Email: ${currentUser.email}`);Base Object
The base.activeCollaborators property returns an array of all Collaborator objects for the current base:
// Get all collaborators for this base
const collaborators = base.activeCollaborators;
output.markdown('# Base Collaborators');
output.text(`Number of collaborators: ${collaborators.length}`);
// List all collaborators
for (const collaborator of collaborators) {
output.text(`- ${collaborator.name || 'Unnamed'} (${collaborator.email})`);
}Use the base.getCollaborator() method to find a specific Collaborator by ID, name, or email:
// Find a collaborator by email
const collaborator = base.getCollaborator('john.doe@example.com');
if (collaborator) {
output.text(`Found collaborator: ${collaborator.name || 'Unnamed'}`);
output.text(`ID: ${collaborator.id}`);
} else {
output.text('Collaborator not found.');
}User Field
For a User field, getCellValue() returns a Collaborator object. If multiple selection is enabled, it returns an array of Collaborator objects:
// Get a record from the Tasks table
const tasksTable = base.getTable('Tasks');
const taskRecord = await tasksTable.selectRecordAsync('123');
// Get the assigned user (assuming 'Assigned To' is a User field)
const assignedTo = taskRecord.getCellValue('Assigned To');
if (assignedTo) {
// This is a Collaborator object
output.text(`Task assigned to: ${assignedTo.name || assignedTo.email}`);
} else {
output.text('Task is unassigned.');
}
// For a User field that allows multiple users
const reviewers = taskRecord.getCellValue('Reviewers') || [];
if (reviewers.length > 0) {
output.text('Reviewers:');
for (const reviewer of reviewers) {
// Each reviewer is a Collaborator object
output.text(`- ${reviewer.name || reviewer.email}`);
}
} else {
output.text('No reviewers assigned.');
}Created By and Last Modified By Fields
The CreatedBy and LastModifiedBy field types return Collaborator objects:
// Get a record with system user fields
const recordsTable = base.getTable('Documents');
const document = await recordsTable.selectRecordAsync('123');
// Get the user who created the record
const createdBy = document.getCellValue('Created By');
if (createdBy) {
output.text(`Created by: ${createdBy.name || createdBy.email}`);
output.text(`Creation date: ${document.getCellValueAsString('Created Time')}`);
}
// Get the user who last modified the record
const lastModifiedBy = document.getCellValue('Last Modified By');
if (lastModifiedBy) {
output.text(`Last modified by: ${lastModifiedBy.name || lastModifiedBy.email}`);
output.text(`Last modified: ${document.getCellValueAsString('Last Modified Time')}`);
}Working with Collaborator Objects
Finding a Specific Collaborator
// Get all collaborators
const collaborators = base.activeCollaborators;
// Find a collaborator by email (case-insensitive)
function findCollaboratorByEmail(email) {
const lowerEmail = email.toLowerCase();
return collaborators.find(collaborator =>
collaborator.email.toLowerCase() === lowerEmail
);
}
// Find a collaborator by name (case-insensitive)
function findCollaboratorByName(name) {
const lowerName = name.toLowerCase();
return collaborators.find(collaborator =>
collaborator.name && collaborator.name.toLowerCase().includes(lowerName)
);
}
// Example usage
const collaborator1 = findCollaboratorByEmail('jane.smith@example.com');
const collaborator2 = findCollaboratorByName('john');
if (collaborator1) {
output.text(`Found by email: ${collaborator1.name || collaborator1.email}`);
}
if (collaborator2) {
output.text(`Found by name: ${collaborator2.name || collaborator2.email}`);
}Checking if Current User Matches a Specific Collaborator
// Get the current user
const currentUser = session.currentUser;
// Check if the current user is a specific person
function isUser(emailOrId) {
if (currentUser.email.toLowerCase() === emailOrId.toLowerCase()) {
return true;
}
if (currentUser.id === emailOrId) {
return true;
}
return false;
}
// Example usage
if (isUser('admin@example.com')) {
output.text('You are the admin user.');
// Show admin-specific content
} else {
output.text('You are not the admin user.');
// Show regular user content
}Best Practices
-
Check for null names - Some users have no name. Before you use
collaborator.name, always check if it is null. Usecollaborator.name || collaborator.emailas a fallback. -
Be careful with personal information - Think about how you show and use collaborator information. Take special care with outputs that other people can see.
-
Handle missing collaborators - In User fields, always check if the value is null before you read Collaborator properties.
-
Consider multi-user fields - A User field can allow multiple selections. Then the value can be an array of Collaborator objects.
-
Use IDs for references - When you update a User field in a record, use the ID of the collaborator as the value. Do not use the full Collaborator object.
Related
Last updated on