简体   繁体   English

如何在saveChanges上捕获错误-Google App Maker

[英]How to catch an error on saveChanges - Google App Maker

I'm trying to catch an error when creating a new record to a data source. 创建新记录到数据源时,我试图捕获一个错误。 (The data source is set to only accept unique values on one of the fields.) (数据源设置为仅接受其中一个字段的唯一值。)

When attempting to force an error the client side script runs and displays the final popup stating that a new record was created successfully. 尝试执行错误时,客户端脚本将运行并显示最后一个弹出窗口,指出已成功创建新记录。 Instead of displaying the popup to state an error creating the record has occurred. 发生了创建记录的错误,而不是显示弹出窗口来说明。

I spoke with one of our senior devs and he explained the issue is due to saveChanges running asynchronously. 我与一位高级开发人员进行了交谈,他解释了这个问题是由于saveChanges异步运行所致。 (The call to save changes runs without any error and only after it completes does it return an error.) (保存更改的调用运行时没有任何错误,只有完成后才返回错误。)

So my question is how do I catch an error after saveChanges completes and display a popup. 所以我的问题是,在saveChanges完成并显示弹出窗口后如何捕获错误。 (If record created successfully or failed.) (如果记录创建成功或失败。)

My Code: 我的代码:

//Creates New Stores
function createNewStores() {  

var newCert = '{"ShopCertificates":[{"Certificate_ID":"","Certificate_Name":"","Valid_From":"","Valid_To":"","Renewal_Date":"","Date_Applied_For_Renewal":"","Date_Compliance_Received":"","Date_Compliance_Issues_Resolved":"","Compliance_Notice_Date":"","Certificate_URL":""}]}';

//Get the Datasource, set it to create mode and create a new blank item.
var createDatasource = app.datasources.Stores.modes.create;
var draft = createDatasource.item;

//Get the selected values from the page.
var brand = app.pages.A_Add_Store.descendants.Dropdown_Brand_Selector.value;
var division = app.pages.A_Add_Store.descendants.Dropdown_Division_Selector.value;
var storeName = app.pages.A_Add_Store.descendants.Dropdown_Stores_Selector.value;

//Set the values of the draft record to be the values entered in the form.
draft.StoreId = parseInt(storeName);
draft.Brand = brand;
draft.Division = division;
draft.Store_Name = storeName;
draft.Cert_JSON = newCert;

//Create the new record in the datasource and save changes to the datasource.
try{
createDatasource.createItem();
app.datasources.Stores.saveChanges();
}
catch(err){
app.popups.Error_Store_Already_Exists.visible = true;
}

//After record is created set values in form to null.
app.pages.A_Add_Store.descendants.Dropdown_Brand_Selector.value = null;
app.pages.A_Add_Store.descendants.Dropdown_Division_Selector.value = null;
app.pages.A_Add_Store.descendants.Dropdown_Stores_Selector.value = null;

//Display Popup stating store has been added.
app.popups.New_Store_Added.visible = true;
}

Assuming that your Stores datasource is set to 'Manual Save' mode the following should work: 假设您的商店数据源设置为“手动保存”模式,则应该可以进行以下操作:

Replace this section of code: 替换此部分代码:

try{
createDatasource.createItem();
app.datasources.Stores.saveChanges();
}
catch(err){
app.popups.Error_Store_Already_Exists.visible = true;
}

With this: 有了这个:

createDatasource.createItem();
app.datasources.Stores.saveChanges({
  success: function() {
    app.popups.New_Store_Added.visible = true;
  },
  failure: function() {
    app.popups.Error_Store_Already_Exists.visible = true;
  }
});

If your datasource is set to auto save then the saveChanges() function will get ignored and you will not be able to pass a call back in that function. 如果您的数据源设置为自动保存,则saveChanges()函数将被忽略,并且您将无法在该函数中传递回叫。 Please reference the asynchronous operations section in the documentation here https://developers.google.com/appmaker/scripting/client#asynchronous_operations . 请在https://developers.google.com/appmaker/scripting/client#asynchronous_operations处参考文档中的异步操作部分。

If this doesn't work for you or you are unable to use this to figure out your code please let me know. 如果这对您不起作用,或者您无法使用它来确定您的代码,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM