View based records update with Javascript and Jquery ajax

I am using view based PUT and it returned “updated success” message for every record, but the field wasn’t actually updated. I tried many different ways but neither of them worked. I am wondering if someone can help me on my code. What should I do for next step? This is the code I am having:

function updateDataTag(record){
$.ajax({
url: “https://api.knack.com/v1/pages/scene_86/views/view_140/records/” + record.id,
// url: “Knack” + record.id,
method: “PUT”,
//cache: false,
headers: {
“X-Knack-Application-Id”: ‘MYAPPID’,
“X-Knack-REST-API-Key”: ‘knack’,
“Authorization”: Knack.getUserToken(),
“Content-Type”: “application/json”
},
data: {
field_180: “Tag”
},
tryCount: 0,
retryLimit: 3,
success: function(response) {
console.log(‘Record updated:’, response); // TODO: response does not have field_180 updated
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
this.tryCount++;
let tryCount = this.tryCount, retryLimit = this.retryLimit, seconds;
if (tryCount <= retryLimit) { //try again
switch(tryCount) {
case 1:
case 2: seconds = 5; break;
case 3: seconds = 10; break; }
let timeout = seconds * 1000;
console.log("Error: " + XMLHttpRequest.status + " " + XMLHttpRequest.statusText + "\nRetry Count: " + tryCount + “\nRetrying in " + seconds + " seconds”)
let ajaxObject = this;
window.setTimeout(function(){
$.ajax(ajaxObject);
}, timeout);
} else {
console.log(“Failed to Capture Record ID”); // Failure Message in Console
}
}
})
}

$(document).on(‘knack-records-render.view_140’, function(event1, view1, records) {
$(document).on(‘knack-cell-update.view_140’, function(event2, view2, updatedRecord) {
// Filter the initially loaded records for the one with the same ID as the updated one
var matchedRecords = records.filter(recordFromRenderEvent => {
return recordFromRenderEvent.field_329 === updatedRecord.field_329 &&
recordFromRenderEvent.field_132 === updatedRecord.field_132 &&
recordFromRenderEvent.field_137 === updatedRecord.field_137 &&
recordFromRenderEvent.field_133 === updatedRecord.field_133 &&
recordFromRenderEvent.field_138 === updatedRecord.field_138 &&
recordFromRenderEvent.field_134 === updatedRecord.field_134 &&
recordFromRenderEvent.field_139 === updatedRecord.field_139 &&
recordFromRenderEvent.id !== updatedRecord.id;
});
for (const record of matchedRecords) {
// $(‘.knTable .field_180’).text(“Tag”);
// $(‘.knTable .field_180’).trigger(“liszt:updated”);
updateDataTag(record);
}

});
});

Thank you!
Jae

Hi @xjie

I am happy to help you through this if you’d like to DM me. I can post the code but there is quite a bit of set up to do as well. The reason for this is that you are trying to do an object based get & put which would mean that you would have to expose your API key. The way to do this is a view based get & put which means you need a view to get the data from and put the data into.

Let me know.

Craig

1 Like