Update List Item SPfx not working
-
06-02-2021 - |
Question
Below is the code, which is not working, shows below error
An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected.
const body: string = {
'__metadata': { 'type': 'SP.Data.InputsListItem' }, 'IsMapped': true
};
this.props.spHttpClient.post(`${this.props.siteUrl}/_api/web/lists/getbytitle('${this.props.listName}')/items(${item.Id})`,
SPHttpClient.configurations.v1,
{
headers: {
'Accept': 'application/json;odata=nometadata',
'Content-type': 'application/json;odata=verbose',
'odata-version': '',
'IF-MATCH': '*',
'X-HTTP-Method': 'MERGE'
},
body: JSON.stringify(body)
});
})
.then((response: SPHttpClientResponse): void => {
this.setState({
status: `Item with ID: ${latestItemId} successfully updated`,
items: []
});
}, (error: any): void => {
this.setState({
status: `Error updating item: ${error}`,
items: []
});
No correct solution
OTHER TIPS
My test code for your reference.
let body: string = JSON.stringify({
'__metadata': { 'type': 'SP.Data.InputsListItem' }, 'IsMapped': true
});
body = body.substring(1, body .length-1);
body = '{' + body +'}';
this.context.spHttpClient.post(`${this.context.pageContext.web.absoluteUrl}/_api/web/lists/getbytitle('Inputs')/items(1)`,
SPHttpClient.configurations.v1,
{
headers: {
'Accept': 'application/json;odata=nometadata',
'Content-type': 'application/json;odata=verbose',
'odata-version': '',
'IF-MATCH': '*',
'X-HTTP-Method': 'MERGE'
},
body: body
})
.then((response: SPHttpClientResponse): void => {
console.log(response);
}, (error: any): void => {
console.log(error);
});
You can do it without Rest with sp-pnp-js package:
import sp from "sp-pnp-js";
sp.web.lists.getByTitle(this.props.listName)
.items.getById(item.Id).update({ IsMapped: true })
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange