cap-js / cds-typer

CDS type generator for JavaScript
Apache License 2.0
29 stars 10 forks source link

[QUESTION] Actions not working with imports after deployment #384

Open MarvinWeitz opened 2 weeks ago

MarvinWeitz commented 2 weeks ago

Question

Hi everyone. I have a problem regarding actions.

I am using a simple test action

service ProcessorService {
    // Some entities

    action test() returns String;
}

that I want to define in my service.ts

import { test } from '#cds-models/ProcessorService'

export class ProcessorService extends ApplicationService {
    async init() {
        this.on(test, (req: Request) => {
            console.log("Test")
        })

        return super.init()
    }
}

This works locally when trying it locally via cds-ts watch but after building the mta via mta build and deploying it to cf via cf deploy test.mtar, the ProcessorService is not able to load (It doesn't show any data in the ListView I am using). It also failes when using require as staed in the docs It does however work when I use the quoted action name this.on("test", (req: Request) => {...}.

What could be the cause of this problem?

daogrady commented 1 week ago

Hi Marvin,

What could be the cause of this problem?

Good question! I tried reproducing your issue using the minimal model you provided. I then ran cds build, and there I see:

// file: gen/srv/@cds-models/ProcessorService/index.js

// This is an automatically generated file. Please do not change its contents manually!
const cds = require('@sap/cds')
const csn = cds.entities('ProcessorService')
// service
const ProcessorService = { name: 'ProcessorService' }
module.exports = ProcessorService
module.exports.ProcessorService = ProcessorService
// events
// actions
module.exports.test = 'test'
// enums

The line module.exports.test = 'test' should facilitate the use of actions in .on(...), as this basically is a variable containing the string 'test'. Can you confirm this file is properly generated in your project?

Best, Daniel

MarvinWeitz commented 1 week ago

Yes, this is the case. I just checked what the .mtar archive contains after performing mbt build. The main files (shortened):

./srv/services.js:

const cds_1 = require("@sap/cds");
const ProcessorService_1 = require("#cds-models/ProcessorService");
class ProcessorService extends cds_1.ApplicationService {
    async init() {
        this.on(ProcessorService_1.test, async (req) => {...}
    }

./package.json:

{
    "imports": {
         "#cds-models/*": "./@cds-models/*/index.js"
    }
}

./@cds-models/ProcessorService/index.js:

// This is an automatically generated file. Please do not change its contents manually!
const cds_1 = __importDefault(require("@sap/cds"));
const __ = __importStar(require("./../_"));
class ProcessorService extends cds_1.default.Service {
}
exports.ProcessorService = ProcessorService;
exports.default = ProcessorService;

With no sign of test in sight...

daogrady commented 1 week ago

Hi Marvin,

this actually looks like the final step of copying the untranspiled JS files over the transpiled ones in the build step has not run properly in your case. Could you please provide a repository with a minimal working sample so I can reproduce the issue for your particular case?

Best, Daniel