Here is an example of my array (I simplified the array but there are about 100 elements and roughly 20 values for each element:
0:
odata.type: "SP.Data.ProductListItem"
Title: "This is Product 1"
Id: 1
1:
odata.type: "SP.Data.ProductListItem"
Title: "Dell Computer"
Id: 2
The url path is mysite.com/product/2
I created a product component which I want to check if and find product/:id
exists in the array and matches the URL to then display that product's information.
So if you were to go to /product/1
' you would see This is Product 1
title with it's coresponding info and if you go to product/2
you would see Dell Computer.
In my code I'm trying to save the state of the array because I'll be adding a form allowing certain users to update this page's information.
The pnp.sp.web.lists.getByTitle("Product")
works, I'm having trouble getting the filter to work.
import * as React from 'react';
import { default as pnp, sp } from "sp-pnp-js";
export class Product extends React.Component<{ match: any }, {
foundProduct: any,
}> {
constructor(props) {
super(props);
this.state = {
foundProduct: [],
};
}
public componentDidMount() {
const foundProduct = pnp.sp.web.lists.getByTitle("Product")
.items
.filter("Id eq '" + this.props.location.pathname, +"'")
.top(1)
.get()
.then((items: any[]) => {
this.setState({ foundProduct: [...foundProduct] });
})
}
public render(): React.ReactElement<{}> {
console.log(this);
return (
<div>
<div className={styles.colHalf}>
{ this.state.foundProduct.ID }
</div>
<div className={styles.colHalf}>
{ this.state.foundProduct.Title}
</div>
</div>
);
}
}