on 2023 Dec 29 7:56 PM
Thanks in advance for helping me.
I have to add some data to Hana entity excelFile. But I don't know how to add it. I'm learning SAP BTP Hana CDS development. I developed an SAP cds app and connected a UI5 app with that. but when trying to add data from the front end it generates some errors as follows.
error
<code>[odata] - POST /odata/v4/my/excelFile
----------------------------creating...................
{
exId: 9876,
exEmail: 'dfg',
exFirst_name: 'dg',
exLast_name: 'fdqf',
exAvatar: 'fg'
}
[cds] - IllegalArgumentError: The key 'exId' does not exist in the given entity
at UriHelper.buildEntityKeys (/home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/utils/UriHelper.js:87:17)
at ResponseHeaderSetter.setLocationHeader (/home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/core/ResponseHeaderSetter.js:102:17)
at SetResponseHeadersCommand.execute (/home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/SetResponseHeadersCommand.js:66:30)
at CommandExecutor._execute (/home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:71:17)
at /home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:81:18
at ConditionalRequestControlCommand.execute (/home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/ConditionalRequestControlCommand.js:48:5)
at CommandExecutor._execute (/home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:71:17)
at /home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:81:18
at /home/user/projects/MyHANAApp/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/DispatcherCommand.js:88:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
id: '1021492',
level: 'ERROR',
timestamp: 1703877998669
}
view1.controller.js
<code>sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/m/MessageToast",
"sap/m/Dialog",
"sap/m/Button",
"sap/m/Input",
"sap/m/VBox",
"sap/m/MessageBox"
],
/**
* @param {typeof sap.ui.core.mvc.Controller} Controller
*/
function (Controller,MessageToast,Dialog, Button, Input, VBox, MessageBox) {
"use strict";
return Controller.extend("project2.controller.View1", {
handleAddButtonPressed : function (oEvent) {
const that = this;
// Create a Dialog
var oDialog = new Dialog({
title: "Add Details",
content: [
new Input({ placeholder: "Id" }),
new Input({ placeholder: "Email" }),
new Input({ placeholder: "First name" }),
new Input({ placeholder: "Second Name" }),
new Input({ placeholder: "avatar"})
],
beginButton: new Button({
text: "Submit",
press: function() {
// Handle the submitted data
var insertData = {
exId: Number(oDialog.getContent()[0].getValue()),
exEmail: oDialog.getContent()[1].getValue(),
exFirst_name: oDialog.getContent()[2].getValue(),
exLast_name: oDialog.getContent()[3].getValue(),
exAvatar: oDialog.getContent()[4].getValue()
};
// console.log(updateData,typeof (updateData))
let sUrl = that.getOwnerComponent().getModel().getServiceUrl() + "excelFile"
$.ajax({
url: sUrl,
method: "POST",
contentType: "application/json",
data: JSON.stringify(insertData),
success: function(data) {
// Handle success, if needed
console.log("Entity updated successfully:"+data);
MessageToast.show("Entity updated successfully"+data.message);
oTable.getModel("localModel").refresh(true);
},
error: function(error) {
// Handle error
MessageBox.error("Error updating entity:"+ error.message);
}
});
// Do something with the values (e.g., validation or further processing)
// Close the Dialog
oDialog.close();
}
}),
endButton: new Button({
text: "Cancel",
press: function() {
// Close the Dialog without processing the data
oDialog.close();
}
}),
afterClose: function() {
// Destroy the Dialog to avoid memory leaks
oDialog.destroy();
}
});
// Open the Dialog
oDialog.open();
},
});
});
cap-module.cds
<code>using namespace someThingName;
entity excelFile{
key exId : Int16;
exEmail : String;
exFirst_name : String;
exLast_name : String;
exAvatar : LargeString;
}
cap-service.js
<code>
const cds = require('@sap/cds');
const db=cds.connect.to('db');
const {Readable,PassThrough} = require('stream');
const XLSX = require('xlsx');
const moment = require('moment');
console.log('------------------hello-----------');
module.exports = cds.service.impl(srv => {
srv.on('CREATE', 'excelFile', async (req) => {
console.log('----------------------------creating...................');
console.log(req.data);
// Create a new record in the excelFile entity
const result = await cds.run(`INSERT INTO MYDB_EXCELFILE VALUES (?,?,?,?,?)`,[Number(req.data.exId), req.data.exEmail, req.data.exFirst_name, req.data.exLast_name, req.data.exAvatar]);
// Respond with a success message
return { success: true, message: `New record inserted into excelFile entity` };
// Respond with a failure message
// return { error: true, message: `Failed to insert record into excelFile entity` };
})
})
Request clarification before answering.
why code yourself for create?
srv.on('CREATE',
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
31 | |
21 | |
16 | |
8 | |
7 | |
6 | |
5 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.