Spring MongoTemplate插入成功或失败

时间:2016-07-04 05:03:19

标签: spring mongodb

如何检查使用$(function AddItemsToCart() { $('#AddToCartBtn').click(function () { var cartData = new Array(); if ($("#PartsTable> tr").length > 0) { var popupMsgTxt = ''; var counter = 0; $('#PartsTable').find('tr').each(function () { var row = $(this); if (row.find('input[type="checkbox"]').is(':checked')) { var cartDataInfo = new Object(); var $tds = $(this).find('td'); cartDataInfo["CustomerReference"] = $tds.eq(0).find('div').text(); cartDataInfo["Quantity"] = $tds.eq(2).find('div').text(); var $hidden_fields = $(this).find('input:hidden'); cartDataInfo["ItemID"] = $hidden_fields.eq(1).val(); cartDataInfo["ItemCode"] = $hidden_fields.eq(0).val(); popupMsgTxt = $tds.eq(0).find('div').text(); cartData[counter] = cartDataInfo; counter = counter + 1; } }); var cartDataArray = { 'cartData': cartData }; var fleetGuid; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "OrderFormServices.asmx/AddItemsToCart", data: JSON.stringify(cartDataArray), dataType: "json", success: function (result) { fleetGuid = result.d; doWork(); console.log(fleetGuid); } }); function doWork(){ var items = [], options = []; //Iterate all td's in first column $('#PartsTable tr td:nth-child(1)').each(function () { items.push($(this).text()); }); //restrict array to unique fleet numbers items = $.unique(items); //iterate unique array and build array of select options $.each(items, function(i, item) { //// THIS IS WHERE I AM GETTING THE 'UNDEFINED' ERROR //// options.push('<tr><td align="left" valign="top">' + item + '</td><input type="hidden" name="GUID" value="'+ fleetGuid +'"><td class="delete" align="center" style="background-color: transparent;"><i class="fa fa-times text-red cur-pon"></i></td></tr>'); }); //finally empty the select and append the items from the array $('#OrderSummaryTbody').append(options.join()); $("#PopupInnerTextModel").text(popupMsgTxt); $("#popup").fadeIn(750).delay(1750).fadeOut(500); } } }); }); 插入集合的值是否已成功保存?

MongoTemplate

在MongoTemplate中插入,保存或任何方法都不会返回true或false。

1 个答案:

答案 0 :(得分:0)

这里会抛出一个异常是方法流程:

template.insert(object) 
|
|
templateinsert(Object objectToSave, String collectionName)  
|
|
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer)
|
|
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass)
|
|

这是insertDBObject的源代码

    protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
    if(LOGGER.isDebugEnabled()) {
        LOGGER.debug("Inserting DBObject containing fields: {} in collection: {}", dbDoc.keySet(), collectionName);
    }

    return this.execute(collectionName, new CollectionCallback() {
        public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            MongoAction mongoAction = new MongoAction(MongoTemplate.this.writeConcern, MongoActionOperation.INSERT, collectionName, entityClass, dbDoc, (DBObject)null);
            WriteConcern writeConcernToUse = MongoTemplate.this.prepareWriteConcern(mongoAction);
            WriteResult writeResult = writeConcernToUse == null?collection.insert(new DBObject[]{dbDoc}):collection.insert(dbDoc, writeConcernToUse);
            MongoTemplate.this.handleAnyWriteResultErrors(writeResult, dbDoc, MongoActionOperation.INSERT);
            return dbDoc.get("_id");
        }
    });
}

在写入失败时会抛出异常,它们是运行时异常。运行时激活由MongoExceptionTranslator翻译 。任何不抛出任何异常的插入都是成功的。