2

I'm building a web application with SAPUI5 which makes available a list of services, that are stored in a MongoDB and available as OData. I followed this guide jaydata-install-your-own-odata-server-with-nodejs-and-mongodb and these are my model.js:

$data.Class.define("marketplace.Service", $data.Entity, null, {
    Id: {type: "id", key: true, computed: true, nullable: false},
    Name: {type: "string", nullable: false, maxLength: 50},
}, null);
$data.Class.defineEx("marketplace.Context", [$data.EntityContext, $data.ServiceBase], null, {
    Services: {type: $data.EntitySet, elementType: marketplace.Service}
});

exports = marketplace.Context;

and server.js:

var c = require('express');
require('jaydata');
window.DOMParser = require('xmldom').DOMParser; 
require('q');
require('./model.js');

var app = c();
app.use(c.query());
app.use(c.bodyParser());
app.use(c.cookieParser());
app.use(c.methodOverride());
app.configure(function() {app.use(app.router);});
app.use(c.session({secret: 'session key'}));
app.use("/marketplace", $data.JayService.OData.Utils.simpleBodyReader());
app.use("/marketplace", $data.JayService.createAdapter(marketplace.Context, function (req, res) {
    return new marketplace.Context({
        name: "mongoDB",
        databaseName: "marketplace",
        address: "localhost",
        port: 27017
    });
}));
app.use("/", c.static(__dirname));
app.use(c.errorHandler());

app.listen(8080);    

The client is developed by using SAPUI5 and these are the parts of the code relative to the odata model creation:

oModel = sap.ui.model.odata.ODataModel("http://localhost:8080/marketplace", false); // connection to the odata endpoint

oModel.setDefaultBindingMode(sap.ui.model.BindingMode.TwoWay);

sap.ui.getCore().setModel(oModel);

The various services are correctly showed in a SAPUI5 table and I'm easily able to insert a new service by using the POST OData.request in this way:

OData.request({
    requestUri: "http://localhost:8080/marketplace/Services",
    method: "POST",
    data: newEntry // json object with the new entry
    },
    function(insertedItem) {
        // success notifier
    },
    function(err) {
        // error notifier
    }
);

and delete a service by using the SAPUI5 function oModel.remove() in this way (oParams is a json object which contains the alert notification functions):

var serviceId = oTable.getRows()[selectedIndex].getCells()[0].getText();

oModel.remove("/Services('" + serviceId + "')", oParams);

Everything works fine but the update request for a single service. I've tried with the functions provided by SAPUI5 (oModel.update or oModel.submitChanges), by using OData.request ("method: PUT"), by creating an ajax PUT request, I also tried to craft PUT request with Fiddler. I always get error 404:

Request URL:http://localhost:8080/marketplace/Services('NTMzZDM3M2JlNjY2YjY3ODIwZjlmOTQ0')
Request Method:PUT
Status Code:404 Not Found

Where can be the problem? I tried with Chrome, IE, and Firefox; same problem... Thanks

Marco
  • 700
  • 1
  • 14
  • 26
  • When pass url http://localhost:8080/marketplace/Services('NTMzZDM3M2JlNjY2YjY3ODIwZjlmOTQ0') with GET Method, does it return the correct entry? – Maya Apr 03 '14 at 14:24
  • Yes! When using GET method with the same URI I correctly get the XML/ATOM of the entry! – Marco Apr 03 '14 at 15:03

1 Answers1

3

Try to update with MERGE verb and pass the modified fields in JSON format inside the BODY

Robesz
  • 1,646
  • 11
  • 13
  • This solves my problem. Thanks! Anyway, do you know why PUT verb generates this error? With MERGE I'm obliged to send the whole entry in JSON format, otherwise previous values will be removed. Am I right? And what if I use PATCH instead of MERGE? – Marco Apr 03 '14 at 15:31
  • https://github.com/jaydata/jaydata/blob/development/JayService/OData/EntitySetProcessor.js see _supportedMethods PATCH works same as MERGE also worth looking at https://openui5.hana.ondemand.com/resources/sap/ui/model/odata/ODataModel-dbg.js for how the UI5 ODataModel handles MERGE in regular and $BATCH not sure if it does send a partial update – Jasper_07 Apr 04 '14 at 12:08
  • So JayData doesn't support PUT method. But I still can't figure out why I get 404 instead of 405! Anyway, thanks... Very useful! – Marco Apr 04 '14 at 15:37