cancel
Showing results for 
Search instead for 
Did you mean: 

CDS Combined Backend (CAP Node.js) - Custom: TypeError: cds.connect(...).serve is not a function

dvvelzen
Participant
0 Kudos

Dear community,

In our old project we used @sap/cds v3.x.x with @sap/cds-odata-v2-adapter-proxy using the implementation script of CDS Combined Backend (CAP Node.js) - Custom.

However when creating a new project with based on the new cds cli (v5.x.x) and adding the same index.js sample script as described on https://www.npmjs.com/package/@sap/cds-odata-v2-adapter-proxy it throws the error:

user: cap-test $ node srv/index
#2.0#2022 01 17 15:31:32:415#+00:00#WARNING#/LoggingLibrary################PLAIN##Dynamic log level switching not available#
[HPM] Proxy created: /  -> http://localhost:4004
[HPM] Proxy rewrite rule created: "^/v2" ~> ""
(node:548) UnhandledPromiseRejectionWarning: TypeError: cds.connect(...).serve is not a function
    at /home/user/projects/cap-test/srv/index.js:15:27
    at Object.<anonymous> (/home/user/projects/cap-test/srv/index.js:19:3)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:548) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:548) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Is there an updated description available, or a version dependency for this to work ?

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

OliverKlemenz
Advisor
Advisor

I strongly recommend to use the integrated version based on a server.js. Otherwise the following example shows which steps need to be done for a custom bootstrapping (basically what default server.js does):

https://cap.cloud.sap/docs/node.js/cds-serve#built-in-server-js

I will update documentation. Thanks.

dvvelzen
Participant
0 Kudos

Thanks for the quick reply oliver.klemenz. I'll await your update and see if we'll change it now or plan it for a later sprint.

/D

OliverKlemenz
Advisor
Advisor
0 Kudos

Alright. I think an updated version would look like this:

const express = require("express");
const cds = require("@sap/cds");
const proxy = require("@sap/cds-odata-v2-adapter-proxy");

const host = "0.0.0.0";
const port = process.env.PORT || 4004;

(async () => {
const app = express();

// OData V2
app.use(proxy());

// OData V4
await cds.connect.to("db");
await cds.serve("all").in(app);

const server = app.listen(port, host, () => console.info(`app is listing at ${host}:${port}`));
server.on("error", error => console.error(error.stack));
})();

Answers (1)

Answers (1)

0 Kudos

UnhandledPromiseRejectionWarning originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). A rejected promise is like an exception that bubbles up towards the application entry point and causes the root error handler to produce that output. It usually happens in async await functions, and there's an easy fix.

const functionName = async (arguments) => {
try {
// Your code here
} catch (error) {
// Handle rejection here
}
};

A nice way to wait for several Promises to resolve to use the Promise.all function. It expects an Array of Promises, and produces a Promise that resolves to an Array containing the values that the individual Promises resolved to. Furthermore, it only resolves after the last Promise resolves. If any of its input Promises rejects, then the entire Promise.all expression rejects as well. It effectively "runs" all of its input processes "at the same time", emulating the classic "fork-join" pattern.