Create New Row in database using Odata - odata

I have an OData Service that is pointing to a table in the database. I want to enter a new record in the table based on the details from user.
I am using the .create method, but the data doesn't seem to get entered in table.
oView=this.getView();
var df = {};
var name = oView.byId("__input3").getValue();
var goal = oView.byId("__area0").getValue();
df.NAME = name;
df.GOAL = goal;
df.TYPE = "type";
df.THEME = "Theme";
var oModel1= new sap.ui.model.odata.ODataModel("/GDH_OData/services/df.xsodata/");
oModel1.create('/Df', df, null, function() {
alert("SUCCESS");
}, function() {
alert("FAIL");
});
Please help where I am going wrong

you're not giving a lot of info, here...
what is the result of your create call ? SUCCESS or FAIL ? if the later, what is the error message returned ? the following error function can help you on this
function(error) {
var message = $(error.response.body).find('message').first().text();
console.log(message);
alert(message);
}
Is the creation code in the backend called (if you put an external breakpoint in your service, do you get the debugger ?) if so, what is the result of debugging step-by-step ?
edit
if there is a "forbidden" response, then you should check the result of transaction su53 for the user. You'll probably have a failed autorization. Then it's just adding the service to your role autorisation.
PS : its better to add your answer to my questions(s) in your question, under a "edit" or "more information" than to put it as another answer. Thus there is only one place to check for the whole problem.
regards

The result is Fail and the error is "Failed to load resource: the server responded with a status of 403 (Forbidden)"

Related

Get Response from CREATE_STREAM

We upload a document from SAPUI5 to our SAP System using the CREATE_STREAM Method of the oData Service in ABAP. The creation of the document works fine.
What we would like to achieve is to get the response back to SAPUI5. Especially when there is an error during the creation of the document in the backend.
In Frontend we use the uploadSet Control.
...oUploadSet.uploadItem(oItem);
In the Backend we create a message with
...lo_message_container->add_message( iv_msg_type = /iwbep/cl_cos_logger=>error
iv_msg_number = '018'
iv_msg_id = lv_msg_id
iv_add_to_response_header = abap_true
)....
We can find the created message in the error protocol of our gateway server (/IWFND/ERROR_LOG). But how can this message be retrieved in SAPUI5 and used in the MessageManger Control?
We tried the onUploadCompleted Control but we can't find any response data there.
Can somebody explain how the response or a message header from the CREAT_STREAM method can be used in SAPUI5?
The "new" UploadSet control is kinda half-baked imo. The response will get lost in some internal method. This internal method will then trigger onUploadCompleted and you get nothing but useless information.
Lucky for us we can easily overwrite this internal stuff. UploadSet has an aggregation Uploader. We have to provide our own Uploader. Problem solved. Here is the line that needs to be modified.
sap.ui.define([
"sap/m/upload/Uploader",
...
], function (Uploader, ...) {
return Uploader.extend("my.custom.control.Uploader", {
uploadItem: function (oItem, aHeaders) {
// beginning of the method. take it from the official sources
oXhr.onreadystatechange = function () {
const oHandler = that._mRequestHandlers[oItem.getId()];
if (this.readyState === window.XMLHttpRequest.DONE && !oHandler.aborted) {
// we need to return the xhr object. it contains the response!
that.fireUploadCompleted({ item: oItem, xhr: oXhr });
}
};
// .. rest of the method
}
});
});
Use it like this
<mvc:View xmlns:custom="my.custom.control" ....>
<UploadSet items="....">
.....
<uploader>
<custom:Uploader uploadUrl="......"
uploadCompleted=".onUploadCompleted"
uploadStarted=".onUploadStarted" />
</uploader>
</UploadSet>
Edit: Your own uploader also means implementing your own event handlers (uploadAborted, uploadCompleted, uploadProgressed, uploadStarted). See the official documentation for more information about the events.

Nodejs - how to get url parameters using socket.io?

I've been studying Nodejs and Socket.io, the problem is I'm struggling on how to get an url parameter (www.example.com/sample/:sampleid) and use it on my socket connection.
Here's an example:
var roomname;
var username;
app.get('/room/:roomname/user/:username', function(req, res){
var room = {
username: req.params.username,
roomname: req.params.roomname
};
roomname = room.roomname;
username = room.username;
res.render('room', room);
});
var users = {};
io.sockets.on('connection', function (socket) {
socket.username = username;
socket.room = roomname;
users[username] = username;
console.log("joining room: " + roomname)
socket.join(roomname);
console.log("emiting to room: " + roomname)
socket.broadcast.to(roomname).emit('newUser', username);
});
I'm simply storing the name on roomname and using it inside io.sockets.on, but I think that's a hack and should be a better way to achieve this...
What's a good way to solve this kind of situations?
Since you're using res.render and you're already passing room as a data parameter, I think it's best to make use of that and do something like this (I use EJS in my example since I'm used to that):
Client:
<script>socket.emit('join', {room: '<%- room.roomname %>' });</script>
Server:
io.on('connection', (socket) => {
socket.room = '';
socket.on('join', (msg) => {
socket.room = msg.room;
});
});
Otherwise it could be an idea to emit document.location.pathname, but then you'd have to parse the path yourself to get the roomname out of it.
EDIT:
What you're doing at the moment is actually very dangerous, because of the following scenario:
User 1 opens the page, "global" variables username and room get set (let's set them both to foo for now), but user 1 has slow internet, so it takes some time to load and for socket.io to connect.
Now User 2 opens the page, the "global" variables username and room are changed to the new values (let's use bar), before user 1 opens up the socket.io connection.
Now User 1 and User 2 connect with socket.io, but since user 2 has faster internet than user 1, var username and var roomname are now both set to value bar even though User 1 has username and roomname foo.
Just a heads up.
socket can emit any event because socket object is EventEmitter so that it can fire(emit) event in any time and on anther side server can listen to this events by .on function
example
socket.emit('join',{name:"ahmed"})
server
io.sockets.on('join',function(data){
console.log(data.name)
})
I had the same question and arrived at a different solution, where I pass the parameters at the time of connection.
//it's my connection in client side
let socket = socketio(`http://localhost:4444?user=${uuid}&room=${room}`);
// where UUID is my user ID and room id
So on the server side we can get that parameters like below
let userUUID = socket.handshake.query.user;
let roomId = socket.handshake.query.room;
The good thing is that when connection is lost you do not depend on the fact that the client will call your custom connection event again, you can put your logic into the default io.on('connection',);
More info here https://socket.io/docs/server-api/#socket-handshake

Parse-server Cloud Code Error 141 Invalid Function

I am running parse-server on Heroku, I am working on implementing a custom cloud code function, however every implementation returns invalid function code: 141, Version:1.12.0
I have successfully gotten the "hello" function to work, including 1 change I made to it:
Parse.Cloud.define('hello', function(req, res) {
console.log("received.........");
res.success('Hi');
});
The custom function I am trying to get working is a simple query to my database:
Parse.Cloud.define("titleQuery", function(request, response) {
var query = new Parse.Query("StudentNotes");
query.equalTo("title", request.params.title);
query.find({
success: function(results) {
console.log("received........." + results);
response.success(results);
},
error: function() {
console.log("received........." + error);
response.error("title lookup failed");
}
});
});
When I run this on iOS with the following code:
PFCloud.callFunctionInBackground("titleQuery", withParameters: ["title": "testTitle"]) {
(response: AnyObject ? , error : NSError ? ) - > Void in
let hello = response // as? String
print(hello)
}
I am querying my database in class "StudentNotes" for object title with the name "testTitle", I know for a fact that that object exists, however due to it throwing error 141 I do not receive anything. Any help would be greatly appreciated.
EDIT2: I have gotten custom cloud functions to work, however I cannot get any queries to my database to work. Can anyone post a confirmed working query that returns an object? Perhaps from the _User table so that I can copy/paste and ensure that my cloud code can access my database?
My process:
I edit the Main.js file and add in my cloud function.
Then i commit & push (successfully)
finally i restart my Heroku server
Then i still get an error 141 invalid function return
I have successfully solved this problem and gotten regular queries to work. The problem was in my Heroku config vars in the dashboard. My server URL was invalid, never changed from the default of "http://yourappname.com/parse/" I have to manually enter "yourappname".

SAPUI5 oModel.create() - how to post data to the SAP backend?

I got a button where I want to post data to my SAP backend on press-method:
oCellBtnOtherchart.addContent(new sap.ui.commons.Button({
text : "Save",
press : function() {
var sServiceUrl = "/MyEntitSet('0001')";
var oModel = sap.ui.getCore().getModel();
console.log(oModel);
var oParameters = {
"email" : "a",
"lastname" : "b",
"firstname" : "c",
};
oModel.create(sServiceUrl, oParameters);
}
}));
My questions are:
In which method would this request end in backend? I expect MyEntitySet_CREATE_ENTITY()
Why doesnt it work, the error message is: HTTP request failed 405, Method Not Allowed
But why is it 405, is my Service URL Wrong? How do I Post data correctly to the SAP Backend?
SAP Troubleshooting Guide says: 405 Method Not Allowed
o The method specified in the Request-Line is not allowed for the resource
identified by the Request-URI. The response must include an Allow header
containing a list of valid methods for the requested resource. --> This does not help me right now, anybody knows how to include an allow header?
Because there are only few threads on this topic at SO, which in my opinion do not answer the questions I had, I'll share my findings how to pass data to the backend via oModels create method:
First Define a type of your result entity (check your oData-Model to know the attributes, e.g. Name and YourID):
var oEntry = {};
oEntry.YourID = "0001";
oEntry.Name = "Peter";
Then fetch your model:
var oModel = sap.ui.getCore().getModel();
Then execute the create operation thanks to: https://sapui5.netweaver.ondemand.com/docs/api/symbols/sap.ui.model.odata.ODataModel.html
jQuery.sap.require("sap.ui.commons.MessageBox");
oModel.create('/EntitySet', oEntry, null, function(){
sap.ui.commons.MessageBox.show(
sap.ui.commons.MessageBox.alert("Success!");
);
},function(){
sap.ui.commons.MessageBox.alert("Error!");
});
Results in Backend in Method "ENTITYSET_CREATE_ENTITY"-Method, where you can retrieve YourID and Name:
DATA: ls_data TYPE ycl_class_mpc=>ts_entity.
CALL METHOD io_data_provider->read_entry_data
IMPORTING
es_data = ls_data.
WRITE ls_data-name.
WRITE ls_data-yourid.
This example applies to single calls, you can see the result in ABAP is a structure. If you need to pass multiple datasets to the backend you should search for batch processing at https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.model.odata.ODataModel.html
If you are still looking for a good blog on how to make a batch post then have a look at this post http://scn.sap.com/community/developer-center/front-end/blog/2012/11/18/gateway-batch-calls-from-sapui5

Unexpected exception upon serializing continuation

I get this error: Unexpected exception upon serializing continuation (not much help)
It is caused by the FetchUrlApp.fetch(); call. I akm using Google Apps Script for Sites, not Google Spreadsheets. The code works in the original instance but as soon as I copy and paste the code into a new project I get the above error message. I am accessing Google Docs APIs. I have read on other forums that I need authorization but I have been unable to gain the right authorization for the code to work. No prompt ever pops up when I run a copy of the code for the first time.
Code exert:
var oauthConfig = UrlFetchApp.addOAuthService("docs");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://docs.google.com/feeds/");
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey(_consumerKey_);
oauthConfig.setConsumerSecret(_consumerSecret_);
var requestData3 = {
"method": "GET",
"headers": {"GData-Version": "3.0"},
"oAuthServiceName": "docs",
"oAuthUseToken": "always",
};
var url = "https://docs.google.com/feeds/" + userName + "/private/full/-/mine";
var result = UrlFetchApp.fetch(url, requestData3); //error occurs, any thoughts?
Thank you in advance,
James Krimm
You have to set both consumerKey and consumerSecret to "anonymous" in order to trigger the 3-legged OAuth process:
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
Replace your two lines with these and the authorization popup dialog will show up, allowing the user to grant access to its documents.
What I would suggest is to write a special function that does nothing else than call the Oauth process and call it from the script editor once.
As an example, here is the one I have used recently to make the authorize popup appear :
function authorize(){
// function to call from the script editor to authorize googleOauth
var id=mailtemplatedoc
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/documents/Export? exportFormat=html&format=html&id='+id,
googleOAuth_('docs',url)).getContentText();
}
EDIT : And the missing part I had forgotten :
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}

Resources