Mar 14, 2019 12:01 PM
I’m hoping someone has run into this before and can help me out. I’m sure I just have a syntax error somewhere or something equally silly.
I’m using jQuery AJAX (I have to, I will explain why if anyone asks).
I am able to retrieve, post, and patch data for everything but my checkbox field. I have sent the value “true” with and without quotes.
Here is my code:
$.ajax({
url: `${Airtable.API}/To%20Do/${recordId}`,
type: 'PATCH',
headers: {
authorization: `Bearer ${Airtable.API_KEY}`,
contentType: "application/json"
},
data: {
"fields": {
"Done": true // does not work as "true" or true
}
}
})
.fail((error) => {
console.error("There was an error processing your request:", error, error.responseText);
})
.done((response) => {
console.log(response);
});
Here is the error:
responseJSON: {
error: {
{
message: "Field Done can not accept value true"
type: "INVALID_VALUE_FOR_COLUMN"
}
}
For reference, this is a simple TODO table. The shape of the data is just:
Name: String,
Notes: String (Long Text),
Done: Boolean
Mar 14, 2019 12:27 PM
You are passing the string “true” not the boolean value true
. Remove the quotes from true in your payload and you should be good to go!
data: {
"fields": {
"Done": true //use the boolean constant true not a string that spells "true"
}
}
Mar 14, 2019 12:30 PM
You are passing the string “true” not the boolean value
true
. Remove the quotes from true in your payload and you should be good to go!
Unfortunately that is not the case, as I noted I have tried to pass it with and without. I did update the code to show that I was passing it as a boolean though, as that is the main way I am trying to send it.
Mar 14, 2019 12:34 PM
I also tried to add typecast true and that gets me the following error:
{"error":{"type":"INVALID_REQUEST_UNKNOWN","message":"Invalid request: parameter validation failed. Check your request data."}}
Here is the code with that typecast property set to true:
$.ajax({
url: `${Airtable.API}/To%20Do/${recordId}`,
type: 'PATCH',
headers: {
authorization: `Bearer ${Airtable.API_KEY}`,
contentType: "application/json"
},
data: {
"fields": {
"Done": true
},
"typecast": true // Adding typecast: true does not work
}
})
.fail((error) => {
console.error("There was an error processing your request:", error, error.responseText);
})
.done((response) => {
console.log(response);
});
Mar 14, 2019 12:42 PM
I was using Postman to test and “true” doesn’t work where true
does. For AJAX, can you try stringifying data? typecast
shouldn’t be necessary in this case.
$.ajax({
url: `${Airtable.API}/To%20Do/${recordId}`,
type: 'PATCH',
headers: {
authorization: `Bearer ${Airtable.API_KEY}`,
contentType: "application/json"
},
data: JSON.stringify({
"fields": {
"Done": true
}
})
})
.fail((error) => {
console.error("There was an error processing your request:", error, error.responseText);
})
.done((response) => {
console.log(response);
});
Mar 14, 2019 12:52 PM
I was using Postman to test and “true” doesn’t work where
true
does. For AJAX, can you try stringifying data?typecast
shouldn’t be necessary in this case.
Thanks for messing with it. Wrapping the fields in JSON.stringify gives me an error because it cannot find fields. Moving it around, say to wrap the field itself, or just the word “true” also gives me errors, ones I’ve been running into.
I am presenting this as a demo for my class to play with it (I teach front end web development). So I’ve gone and changed it into a dropdown menu for now with the values “Yes” and “No”.
In the meantime though, this is starting to smell an awful lot like a bug!
Mar 14, 2019 01:08 PM
Hmm, your code looks correct, and I can assure you it does work. What happens if you use a 1 or 0 instead of true/false?
That’s cool you’re teaching Airtable in class. You should join a podcast on BuiltOnAir :slightly_smiling_face:
Mar 14, 2019 01:14 PM
Blockquote Hmm, your code looks correct, and I can assure you it does work. What happens if you use a 1 or 0 instead of true/false?
Same thing.
That’s cool you’re teaching Airtable in class. You should join a podcast on BuiltOnAir :slightly_smiling_face:
Thanks! I will check that out :grinning_face_with_big_eyes:
Mar 14, 2019 01:22 PM
I think I have insight into the problem, and it is definitely me.
I use a live server through Visual Studio Code or I serve through my file system. The students are very new to JS/jQuery, so I haven’t implemented any npm/webpack stuff. This is all pretty simple stuff.
I don’t have a self-signed certificate made yet, so my “live server” just serves at http://local-ip. It’s simple enough for most of my needs.
But ya’ll probably know what I’m getting at. Due to the limitations of what I show this class and my fake server, I have to use an browser extension to spoof Allow CORS. This prevents me from getting the CORS error so common with local hosting, and allows me to work without creating a self-signed certificate. As for working with Airtable’s API, this has been a solid solution to that problem until now.
Thanks to the investigative work by @Giovanni_Briggs and @openside I know that my code is correct and will submit booleans under the right circumstances. But something about my CORS submission prevents me from submitting any data that isn’t wrapped in a string.
So for now I shall stick with the drop down menu to keep things simple for my students. For anyone lucky enough to run into the same problem, you need to fix the path you’re serving from to be able to send data other than strings.
Hope that makes sense!