Javascript stored procedure works in production but fails to execute in emulator

Warren Buckley (wbuckley) 1 Reputation point
2022-10-19T20:38:02.793+00:00

We have a stored procedure which works without issue when running live on Cosmos DB in Azure.
But running the same stored procedure on the emulator fails with the following error:

The delegate parameter is incorrect. Make sure that the delegate is function declaration inlined with the call.","Encountered exception while executing Javascript. Exception = Error: filter: error
processing delegate function.\r\nStack trace: Error: filter: error processing delegate function.\n

The Javascript function that's throwing the above error is:

function fetchProjectLinks(container, organizationId) {  
    return new Promise((resolve, reject) => {  
        const predicate = (doc) =>  
            doc.type === 'project'  
            && doc.organizationId === organizationId;  
  
        const result = container.chain().filter(predicate).map(doc => doc._self) // <-- This is the line it points to.  
            .value((error, docs) => {  
                if (error) { reject(error); }  
                else { resolve(docs); }  
            });  
  
        if (!result.isAccepted) {  
            reject(new Error('Failed to fetch project document links.'));  
        }  
    });  
}  

Currently I'm attempting to use the emulator for integration testing.
The emulator is the latest Linux docker container (which I'm running on Windows).

Doesn't matter if I inline the predicate or not, I always get the same error on the emulator.

My question is, why does this function throw the above error when running in the emulator but runs perfectly when run in Azure.
Is the Javascript being interpreted differently?
Is there a difference between the Javascript API's?

Here is the full Stored Procedure:

/*global getContext, ErrorCodes*/  
  
// eslint-disable-next-line no-unused-vars  
function createDomainControllers(controllers) {  
    if (controllers == null) {  
        throw new Error(ErrorCodes.BadRequest,  
            'The parameter "controllers" cannot be null or undefined');  
    }  
  
    if (typeof controllers === 'string') {  
        controllers = JSON.parse(controllers);  
    }  
  
    if (!Array.isArray(controllers)) {  
        controllers = [controllers];  
    }  
  
    const context = getContext();  
  
    async function main() {  
        const container = context.getCollection();  
  
        let promises = controllers.map(c => createController(container, c));  
        controllers = await Promise.all(promises);  
  
        const [first] = controllers;  
        const links = await fetchProjectLinks(container, first.organizationId);  
  
        promises = links.map(link => patchProject(container, link, controllers));  
        await Promise.all(promises);  
  
        return controllers;  
    }  
  
    main().then(dcs => context.getResponse().setBody(dcs))  
        .catch(error => context.abort(error));  
}  
  
function createController(container, controller) {  
    return new Promise((resolve, reject) => {  
        const link = container.getSelfLink();  
        const options = { disableAutomaticIdGeneration: false };  
  
        const created = container.createDocument(link, controller, options, (error, dc) => {  
            if (error) { reject(error); }  
            else { resolve(dc); }  
        });  
  
        if (!created) {  
            reject(new Error('Failed to create domain controller document.'));  
        }  
    });  
}  
  
function patchProject(container, link, controllers) {  
    return new Promise((resolve, reject) => {  
        const patch = [];  
        for (const dc of controllers) {  
            const item = { dc: dc };  
            patch.push({ 'op': 'add', 'path': '/workItems/-', 'value': item });  
        }  
  
        const result = container.patchDocument(link, patch, (error, doc) => {  
            if (error) { reject(error); }  
            else { resolve(doc); }  
        });  
  
        if (!result) {  
            reject(new Error('Failed to replace project document.'));  
        }  
    });  
}  
  
function fetchProjectLinks(container, organizationId) {  
    return new Promise((resolve, reject) => {  
        const predicate = (doc) =>  
            doc.type === 'project'  
            && doc.organizationId === organizationId;  
  
        const result = container.chain().filter(predicate).map(doc => doc._self)  
            .value((error, docs) => {  
                if (error) { reject(error); }  
                else { resolve(docs); }  
            });  
  
        if (!result.isAccepted) {  
            reject(new Error('Failed to fetch project document links.'));  
        }  
    });  
}  

Here is the Domain Controller document:

{  
    "id": "d4f6300d-506f-8c8c-276d-ec348cc511f9",  
    "organizationId": "7239f2ad-3fc7-40b2-b966-2e5c9b0bc59d",  
    "dnsName": "berniece.info",  
    "domainsDnsName": "chad.berniece.info",  
    "site": "solutions",  
    "isReadOnly": false,  
    "ntdsGuid": null,  
    "fsmoRoles": [  
        "schemaMaster",  
        "ridMaster"  
    ],  
    "type": "controller"  
}  

Here is the Project document:

{  
    "id": "f8ca8363-7e78-4279-8ef6-ea4d1eed6a38",  
    "organizationId": "6caca5ea-5e02-4456-a683-419b89d5131c",  
    "version": null,  
    "projectName": "Test project 2",  
    "recoveryMode": "forestRecovery",  
    "forest": "Forest2",  
    "workItems": [  
        {  
            "dc": {  
                "id": "3e130b16-4bc3-44dd-b929-ec393eed28e5",  
                "organizationId": "6caca5ea-5e02-4456-a683-419b89d5131c",  
                "dnsName": "dc1.rmad.local",  
                "domainsDnsName": "rmad.local",  
                "site": "Rmad site",  
                "isReadOnly": false,  
                "ntdsGuid": null,  
                "fsmoRoles": [  
                    "pdcEmulator",  
                    "infrastructureMaster"  
                ],  
                "type": "controller"  
            },  
            "credentials": null,  
            "computerInformation": null,  
            "restoreSysvolFromBackup": false,  
            "isRestoreAdSecondStage": false,  
            "wipeDisks": false,  
            "performMalwareScan": false,  
            "operationsToPause": null,  
            "isEnabled": false,  
            "recoveryAction": "none",  
            "backup": null,  
            "autoUpdateBackup": false,  
            "dnsChangeSet": null,  
            "dnsChange": null,  
            "keepGcDataUnchanged": false,  
            "configureAsGcAfterReinstall": false,  
            "promoteRodc": false,  
            "tempStorage": null,  
            "isProcessed": false,  
            "useAuthoritativeSysvolRestore": false,  
            "inheritDomainSettings": false,  
            "domotionParameters": null,  
            "agentInformation": null,  
            "ditPath": null,  
            "logPath": null,  
            "sysvolPath": null,  
            "useAdPathsFromBackup": false,  
            "targetIpAddress": null,  
            "useFeAgentPort": null,  
            "feAgentPort": null,  
            "isoIpAddress": null,  
            "isoSubnetMask": null,  
            "isoDefaultGateway": null,  
            "useOriginalTargetNetworkSettings": false,  
            "isoPath": null,  
            "saveIsoOnBackupShare": false,  
            "driversPath": null,  
            "addCustomDrivers": false,  
            "addCustomDriversFromBackup": false,  
            "keepInProject": false  
        },  
        {  
            "dc": {  
                "id": "f37a197a-c7f0-4f00-8fd9-40e31386ab64",  
                "organizationId": "6caca5ea-5e02-4456-a683-419b89d5131c",  
                "dnsName": "dc2.rmad.local",  
                "domainsDnsName": "rmad.local",  
                "site": "Rmad site",  
                "isReadOnly": false,  
                "ntdsGuid": null,  
                "fsmoRoles": [  
                    "ridMaster",  
                    "domainNamingMaster"  
                ],  
                "type": "controller"  
            },  
            "credentials": null,  
            "computerInformation": null,  
            "restoreSysvolFromBackup": false,  
            "isRestoreAdSecondStage": false,  
            "wipeDisks": false,  
            "performMalwareScan": false,  
            "operationsToPause": null,  
            "isEnabled": false,  
            "recoveryAction": "none",  
            "backup": null,  
            "autoUpdateBackup": false,  
            "dnsChangeSet": null,  
            "dnsChange": null,  
            "keepGcDataUnchanged": false,  
            "configureAsGcAfterReinstall": false,  
            "promoteRodc": false,  
            "tempStorage": null,  
            "isProcessed": false,  
            "useAuthoritativeSysvolRestore": false,  
            "inheritDomainSettings": false,  
            "domotionParameters": null,  
            "agentInformation": null,  
            "ditPath": null,  
            "logPath": null,  
            "sysvolPath": null,  
            "useAdPathsFromBackup": false,  
            "targetIpAddress": null,  
            "useFeAgentPort": null,  
            "feAgentPort": null,  
            "isoIpAddress": null,  
            "isoSubnetMask": null,  
            "isoDefaultGateway": null,  
            "useOriginalTargetNetworkSettings": false,  
            "isoPath": null,  
            "saveIsoOnBackupShare": false,  
            "driversPath": null,  
            "addCustomDrivers": false,  
            "addCustomDriversFromBackup": false,  
            "keepInProject": false  
        },  
        {  
            "dc": {  
                "id": "2d608e27-7af4-4076-a031-21206fa7e3c5",  
                "organizationId": "6caca5ea-5e02-4456-a683-419b89d5131c",  
                "dnsName": "dc3.rmad.local",  
                "domainsDnsName": "rmad.local",  
                "site": "Rmad site",  
                "isReadOnly": false,  
                "ntdsGuid": null,  
                "fsmoRoles": [],  
                "type": "controller"  
            },  
            "credentials": null,  
            "computerInformation": null,  
            "restoreSysvolFromBackup": false,  
            "isRestoreAdSecondStage": false,  
            "wipeDisks": false,  
            "performMalwareScan": false,  
            "operationsToPause": null,  
            "isEnabled": false,  
            "recoveryAction": "none",  
            "backup": null,  
            "autoUpdateBackup": false,  
            "dnsChangeSet": null,  
            "dnsChange": null,  
            "keepGcDataUnchanged": false,  
            "configureAsGcAfterReinstall": false,  
            "promoteRodc": false,  
            "tempStorage": null,  
            "isProcessed": false,  
            "useAuthoritativeSysvolRestore": false,  
            "inheritDomainSettings": false,  
            "domotionParameters": null,  
            "agentInformation": null,  
            "ditPath": null,  
            "logPath": null,  
            "sysvolPath": null,  
            "useAdPathsFromBackup": false,  
            "targetIpAddress": null,  
            "useFeAgentPort": null,  
            "feAgentPort": null,  
            "isoIpAddress": null,  
            "isoSubnetMask": null,  
            "isoDefaultGateway": null,  
            "useOriginalTargetNetworkSettings": false,  
            "isoPath": null,  
            "saveIsoOnBackupShare": false,  
            "driversPath": null,  
            "addCustomDrivers": false,  
            "addCustomDriversFromBackup": false,  
            "keepInProject": false  
        }  
    ],  
    "infrastructureOrigin": null,  
    "infrastructureUpdatedOn": "0001-01-01T00:00:00",  
    "password": null,  
    "passwordHash": null,  
    "disableAuthoritativeSysvolRestore": [],  
    "excludedDomains": [  
        "quest.local",  
        "microsoft.com"  
    ],  
    "infrastructureTemplates": [],  
    "pauses": [],  
    "alerts": [],  
    "gcResetStategy": "reset",  
    "backupFilterCriteria": {  
        "searchBackupsFromNow": false,  
        "backupAge": 0,  
        "backupsFromDcPreferabled": false,  
        "beforeDate": "2022-10-08T16:05:47.5792292Z",  
        "afterDate": "2022-09-29T16:05:47.5791721Z"  
    },  
    "bmrBackupFilterCriteria": null,  
    "maximumBackupsAgeDifference": 0,  
    "dcComputerDataUpdatedOn": "0001-01-01T00:00:00",  
    "testScenarioFileName": null,  
    "domainsSettings": [],  
    "defaultDomainsSettings": [],  
    "resetAdminPasswordSettings": null,  
    "isSimulationProject": false,  
    "dnsCacheData": "",  
    "type": "project"  
}  

Here is the C# Cosmos SDK Usage:

public async Task
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,496 questions
{count} votes