I have a form that contains check boxes and input fields, and hitting the Submit button should ideally add the content values to a SP list.
When I run the debugger I can see that each input (checked items, text inputs, etc) is getting passed into addItemToSPList
, but when I open the Network tab I see the following error:
"value: "A node of type 'StartArray' was read from the JSON reader when trying to read a value of a property; however, a 'PrimitiveValue' or 'StartObject' node was expected."
I am suspecting that something's wrong within my ajax
block (perhaps the URL), but I'm not 100% sure.
Any thoughts on what's going on?
JS code:
handleClick() {
let specialtiesArr = [],
regionsArr = [],
commentsArr = [],
nameArr = [];
$(".check-spec:checked").each(function() {
specialtiesArr.push($(this).val());
})
$(".check-region:checked").each(function() {
regionsArr.push($(this).val());
})
commentsArr.push($('.request-text-area').val());
nameArr.push($('.submitter-name').val());
addItemToSPList(specialtiesArr, regionsArr, commentsArr, nameArr)
}
} // export default class closing bracket
function addItemToSPList(getSpecialties, getRegions, getComments, getSubmitterName) {
let specialistRequestsColumns = {
"__metadata":{"type": "SP.Data.Specialist_x0020_RequestsListItem"},
"Title": "No Title", // marked it as "not required" in the SP list backend
"Specialties": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: getSpecialties }, // multi-select
"Regions": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: getRegions }, // multi-select
"Comments": getComments, // text box
"Submitter_x0020_Name": getSubmitterName // input field
}
let listName = "Specialist%20Requests";
$.ajax({
url: `${_BaseHost}/redacted/_api/web/lists/getByTitle('${listName}')/items`, // ?$select=ListItemEntityTypeFullName
method: "POST", // type
async: false,
contentType: "application/json;odata=verbose",
data: JSON.stringify(specialistRequestsColumns),
xhrFields: {
withCredentials: true
},
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: data => {
console.log("upload successful")
// console.log(data)
},error: (err) => {
console.log("Error: " + err);
}
})
}
getSpecialties
andgetRegions
? Is there any other required column in your list for which you are not passing any value?getSpecialties
andgetRegions
are checked checkboxes, and when a box is checked it's added to an array (I added the rest of thehandleClick()
code above). Also, all of the columns are listed above and none are required.Comments
orSubmitterName
(or both). By your code comments you seem to be implying that theComments
field is a text input, not a collection, so I would think that SharePoint is expecting a string (primitive value), yet you are putting that single string into an arraycommentsArr.push($('.request-text-area').val());
before sending it to SP.