【发布时间】:2019-06-12 13:12:12
【问题描述】:
我在 Mongo DB 中保存客户记录,我使用 Angular 6 作为前端。 保存时,我没有发送 Id 值,所以 Mongo 会自动创建 id 并保存记录。
我在 Java 中使用 MongoRepository 进行保存。但是在执行“deleteById”或“findById”时,它无法搜索或删除这些记录。
你能帮忙吗?
Angular 客户模型
export interface Customer {
id : string,
custId : number,
customerName : string,
email: string,
phone : string,
age: number,
city : string,
state : string,
createdDate : Date
}
用户.service.ts
deleteCustomerData(id): Observable<Customer>{
console.log(this.deleteCustomerUrl + id);
return this.http.delete<Customer>(this.deleteCustomerUrl + id);
}
Java 控制器
@DeleteMapping("/deleteCustomer/{id}")
public String deleteCustomerById(@PathVariable String id) {
//ObjectId objId = new ObjectId(id);
customerService.deleteCustomerById(id);
return "deleted customer by id"+ id;
}
Java 服务
public void deleteCustomerById(String id) {
customerRepository.deleteById(id);
}
Java 模型
@Document(collection="Customer")
public class Customer {
@Id
private String Id;
private String customerName;
private int age;
private String city;
private String state;
private int custId;
}
Java 存储库
package com.tivo.extract.config.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.tivo.extract.config.model.Customer;
public interface CustomerRepository extends MongoRepository<Customer, String>{
}
【问题讨论】:
-
请检查您是否已从前端向 API 服务器发送 DELETE 请求。如果不是这种情况,请在请求触发时附上异常或日志。
-
没有例外,我要发送什么... db 中没有匹配项...但也不例外。
-
我存在于 db 和 UI 中,我可以发送该 ID,但不知道为什么找不到它。可能是因为它是一个 objectId 什么的。
-
java 端一切正常,请检查角度。
标签: spring mongodb rest angular6 mongorepository