Before starting, sorry if this is a very elemental question and if I’m missing something basic. I’m trying to create a three tier app in which users can create and consult purchase orders. So far everything is working, except for some methods that I have to update information on orders. When I click the “Update”, “Delete” or “Complete Status” buttons in the front end, I should get a message saying “The order was successfully updated!”, but instead, nothing happens and I get this error in my Java back end:
Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: "undefined"]
Here is all the relevant code that I have for the back end:
Order Model
package com.backend.springjwt.models;
import javax.persistence.*;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ordid_generator")
@SequenceGenerator(name = "ordid_generator", initialValue = 10000, allocationSize = 1, sequenceName = "ordid_seq")
private long id;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Column(name = "completed")
private boolean completed;
@Column(name = "userid")
private String userId;
public Order() {
}
public Order(String title, String description, boolean completed, String userId) {
this.title = title;
this.description = description;
this.completed = completed;
this.userId = userId;
}
Order Controller
@PutMapping("/orders/update/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Order> updateOrder(@PathVariable("id") long id, @RequestBody Order order) {
Optional<Order> orderData = orderRepository.findById(id);
if (orderData.isPresent()) {
Order _order = orderData.get();
_order.setTitle(order.getTitle());
_order.setDescription(order.getDescription());
_order.setCompleted(order.isCompleted());
_order.setUserId(order.getUserId());
System.out.println(order.getId());
System.out.println(order.getDescription());
System.out.println(order.getUserId());
return new ResponseEntity<>(orderRepository.save(_order), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
Here is all the relevant code that I have for the front end:
Order Component
import React, { Component } from "react";
import OrderDataService from "../services/order.service";
export default class Order extends Component {
constructor(props) {
super(props);
this.onChangeTitle = this.onChangeTitle.bind(this);
this.onChangeDescription = this.onChangeDescription.bind(this);
this.onChangeUserId = this.onChangeUserId.bind(this);
this.getOrder = this.getOrder.bind(this);
this.updatecompleted = this.updatecompleted.bind(this);
this.updateOrder = this.updateOrder.bind(this);
this.deleteOrder = this.deleteOrder.bind(this);
this.state = {
currentOrder: {
id: null,
title: "",
description: "",
completed: false,
userId: ""
},
message: ""
};
}
componentDidMount() {
this.getOrder(this.props.match.params.id);
}
onChangeTitle(e) {
const title = e.target.value;
this.setState(function(prevState) {
return {
currentOrder: {
...prevState.currentOrder,
title: title
}
};
});
}
onChangeDescription(e) {
const description = e.target.value;
this.setState(prevState => ({
currentOrder: {
...prevState.currentOrder,
description: description
}
}));
}
onChangeUserId(e) {
const userId = e.target.value;
this.setState(prevState => ({
currentOrder: {
...prevState.currentOrder,
userId: userId
}
}));
}
getOrder(id) {
OrderDataService.getById(id)
.then(response => {
this.setState({
currentOrder: response.data
});
console.log(response.data);
})
.catch(e => {
console.log(e);
});
}
updatecompleted(status) {
var data = {
id: this.state.currentOrder.id,
title: this.state.currentOrder.title,
description: this.state.currentOrder.description,
completed: status,
userId: this.state.currentOrder.userId
};
OrderDataService.update(this.state.currentOrder.id, data)
.then(response => {
this.setState(prevState => ({
currentOrder: {
...prevState.currentOrder,
completed: status
}
}));
console.log(response.data);
})
.catch(e => {
console.log(e);
});
}
updateOrder() {
OrderDataService.update(
this.state.currentOrder.id,
this.state.currentOrder
)
.then(response => {
console.log(response.data);
this.setState({
message: "The order was updated successfully!"
});
})
.catch(e => {
console.log(e);
});
}
deleteOrder() {
OrderDataService.delete(this.state.currentOrder.id)
.then(response => {
console.log(response.data);
this.props.history.push('/orders')
})
.catch(e => {
console.log(e);
});
}
render() {
const { currentOrder } = this.state;
return (
<div>
{currentOrder ? (
<div className="edit-form">
<h4>Order</h4>
<form>
<div className="form-group">
<label htmlFor="title">Title</label>
<input
type="text"
className="form-control"
id="title"
value={currentOrder.title}
onChange={this.onChangeTitle}
/>
</div>
<div className="form-group">
<label htmlFor="description">Description</label>
<input
type="text"
className="form-control"
id="description"
value={currentOrder.description}
onChange={this.onChangeDescription}
/>
</div>
<div className="form-group">
<label htmlFor="userId">User ID</label>
<input
type="text"
className="form-control"
id="userId"
value={currentOrder.userId}
onChange={this.onChangeUserId}
/>
</div>
<div className="form-group">
<label>
<strong>Status:</strong>
</label>
{currentOrder.completed ? "completed" : "Pending"}
</div>
</form>
{currentOrder.completed ? (
<button
className="badge badge-primary mr-2"
onClick={() => this.updatecompleted(false)}
>
Change Status
</button>
) : (
<button
className="badge badge-primary mr-2"
onClick={() => this.updatecompleted(true)}
>
Complete Status
</button>
)}
<button
className="badge badge-danger mr-2"
onClick={this.deleteOrder}
>
Delete
</button>
<button
type="submit"
className="badge badge-success"
onClick={this.updateOrder}
>
Update
</button>
<p>{this.state.message}</p>
</div>
) : (
<div>
<br />
<p>Please click on an Order...</p>
</div>
)}
</div>
);
}
}
Order Service
import authHeader from './auth-header';
import http from "../http-common";
class OrderDataService {
getAll() {
return http.get(`/orders/getAll`, { headers: authHeader() });
}
getById(id) {
return http.get(`/orders/get/id/${id}`, { headers: authHeader() });
}
getByUserId(id) {
return http.get(`/orders/get/userid/${id}`, { headers: authHeader() });
}
getByTitle(title) {
return http.get(`/orders/get/title/${title}`, { headers: authHeader() });
}
getByCompleted() {
return http.get(`/orders/get/completed/`, { headers: authHeader() });
}
create(data) {
return http.post(`/orders/create`, data, { headers: authHeader() });
}
update(id, data) {
return http.put(`/orders/update/${id}`, data, { headers: authHeader() });
}
delete(id) {
return http.delete(`/orders/delete/${id}`, { headers: authHeader() });
}
deleteAll() {
return http.delete(`/orders/deleteAll`, { headers: authHeader() });
}
}
export default new OrderDataService();
I’ve been looking at everything for hours and I can’t quite figure out what’s wrong. Since the only long value in the orders table is the id for the order, that must be the issue, but I’ve tried to get the id as a String and then parse it to be long, but I get the same error. Any insight is appreciated.