0

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);
            }
        })
    }
  • What is stored in getSpecialties and getRegions? Is there any other required column in your list for which you are not passing any value? – Ganesh Sanap Jun 25 at 4:32
  • @GaneshSanap The values of getSpecialties and getRegions are checked checkboxes, and when a box is checked it's added to an array (I added the rest of the handleClick() code above). Also, all of the columns are listed above and none are required. – Tsardines Jun 25 at 13:17
  • 1
    At first glance I would say it might be either the Comments or SubmitterName (or both). By your code comments you seem to be implying that the Comments 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 array commentsArr.push($('.request-text-area').val()); before sending it to SP. – Dylan Cristy Jun 25 at 13:44
  • @DylanCristy that's exactly what happened, and both you and Ganesh were on the money. Thank you! – Tsardines Jun 25 at 13:59
1

I think there is an issue with single line of text fields in your code. Try using below code for Comments and Submitter Name:

var comments, submitterName;
comments = $('.request-text-area').val();
submitterName = $('.submitter-name').val();

addItemToSPList(specialtiesArr, regionsArr, comments, submitterName):
| improve this answer | |
  • 1
    Yep, that did it. I was too focused on the addItemToSPList block and didn't really think about the handleClick(). Thank you! – Tsardines Jun 25 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.