Odata 在UI5中插入后获取生成的ID
我的问题是,我需要获取已创建条目的生成ID。 我想在我的Odata 在UI5中插入后获取生成的ID,odata,sapui5,Odata,Sapui5,我的问题是,我需要获取已创建条目的生成ID。 我想在我的ODataModel中创建一个条目,然后再创建另一个条目。第二个应将第一个的ID作为属性保存 但是,当我插入第一个带有{“Id”:“0”}的条目时,自动生成Id,我无法获取响应头以将生成的Id保存在变量中 我试图从成功回调中保存响应,但由于异步性,变量保持未定义状态。同时我自己解决了这个问题。 我将创建第二个条目移动到第一个条目的成功回调中。 因此,我能够从“response”参数中获取ID,并将其作为属性添加到第二个条目中。 这可能不是最
ODataModel
中创建一个条目,然后再创建另一个条目。第二个应将第一个的ID作为属性保存
但是,当我插入第一个带有{“Id”:“0”}
的条目时,自动生成Id,我无法获取响应头以将生成的Id保存在变量中
我试图从成功回调中保存响应,但由于异步性,变量保持未定义状态。同时我自己解决了这个问题。 我将创建第二个条目移动到第一个条目的成功回调中。 因此,我能够从“response”参数中获取ID,并将其作为属性添加到第二个条目中。 这可能不是最好的解决方案,但对我来说很有效。 也许其他人有另一个更好的建议 这只是一个示例代码,但可能会帮助一些遇到类似问题的人
// oView = this.getView();
// First Entry for EntitySampleSet1
var oEntry = {};
oEntry.Id = "0";
oEntry.Label = oView.byId("label").getValue();
oEntry.Status = oView.byId("status").getValue();
// Success Callback for the first Entry Creation
// response contains the Response Header for the POST Request
var fnSuccessCallback = function(oData, response)
{
console.log("Success 1");
// Success Callback for the second create methode
var fnSuccess2Callback = function()
{
console.log("Success 2");
};
// Error Callback for the second create methode
var fnError2Callback = function()
{
console.log("Error 2");
};
// Second Entry for EntitySampleSet2
var o2Entry = {};
// ID = "0" so the ID gets automatically generated by JPA
o2Entry.Id = "0";
o2Entry.SampleRelatedObject = response.data.Id;
// The second Entry gets created in the OData Model
oModel.create("/SampleEntitySet2", o2Entry,
{
urlParameters : null,
success : fnSuccess2Callback,
error : fnError2Callback
});
};
var fnErrorCallback = function(oError)
{
console.log("Error1");
};
// The first Entry gets created in the OData Model
// after the create worked fine the fnSuccessCallback will get
// called and the second entry will be created in that method
oModel.create("/SampleEntitySet1", oEntry,
{
urlParameters : null,
success : fnSuccessCallback,
error : fnErrorCallback
});
同时我自己解决了这个问题。 我将创建第二个条目移动到第一个条目的成功回调中。 因此,我能够从“response”参数中获取ID,并将其作为属性添加到第二个条目中。 这可能不是最好的解决方案,但对我来说很有效。 也许其他人有另一个更好的建议 这只是一个示例代码,但可能会帮助一些遇到类似问题的人
// oView = this.getView();
// First Entry for EntitySampleSet1
var oEntry = {};
oEntry.Id = "0";
oEntry.Label = oView.byId("label").getValue();
oEntry.Status = oView.byId("status").getValue();
// Success Callback for the first Entry Creation
// response contains the Response Header for the POST Request
var fnSuccessCallback = function(oData, response)
{
console.log("Success 1");
// Success Callback for the second create methode
var fnSuccess2Callback = function()
{
console.log("Success 2");
};
// Error Callback for the second create methode
var fnError2Callback = function()
{
console.log("Error 2");
};
// Second Entry for EntitySampleSet2
var o2Entry = {};
// ID = "0" so the ID gets automatically generated by JPA
o2Entry.Id = "0";
o2Entry.SampleRelatedObject = response.data.Id;
// The second Entry gets created in the OData Model
oModel.create("/SampleEntitySet2", o2Entry,
{
urlParameters : null,
success : fnSuccess2Callback,
error : fnError2Callback
});
};
var fnErrorCallback = function(oError)
{
console.log("Error1");
};
// The first Entry gets created in the OData Model
// after the create worked fine the fnSuccessCallback will get
// called and the second entry will be created in that method
oModel.create("/SampleEntitySet1", oEntry,
{
urlParameters : null,
success : fnSuccessCallback,
error : fnErrorCallback
});