我使用一个调用 API 的库,该 API 返回一个对象数组:
new Promise((resolve) => {
Kit.getSamples(walkingSample, (err, results) => {
if (err) {
return resolve([])
}
this.setState({
ActivityItem: results
})
})
})
Run Code Online (Sandbox Code Playgroud)
其中 ActivityItem 是一个在 state 中定义的空数组。现在,这将返回如下内容:
[{
"end": "2020-04-25T21:45:00.000+0200",
"quantity": 11,
"sourceName": "Health",
"start": "2020-04-25T21:45:00.000+0200",
"tracked": false
},
{
"end": "2020-04-25T21:41:00.000+0200",
"quantity": 21,
"sourceName": "Health",
"start": "2020-04-25T21:41:00.000+0200",
"tracked": false
}
]
Run Code Online (Sandbox Code Playgroud)
这是 API 返回的内容,这是存储在我的 ActivityItem 数组中的内容。但是除了 API 返回的内容之外,我还需要添加一个像“name”这样的属性来区分不同的未来调用。我想知道是否有任何方法可以向该数组添加一些属性,使其看起来像这样:
[{
"name": 'Walking',
end ": "
2020 - 04 - 25 T21: 45: 00.000 + 0200 ", …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写本机查询以从基于 EnumType 实体的表中进行搜索。此 ENUM MealType 是 @Table Meal 的一部分。
@Column(name = "meal_type")
@Enumerated(EnumType.STRING)
private MealType mealType;
Run Code Online (Sandbox Code Playgroud)
现在,我的查询是:
@Repository
public interface MealRepository extends JpaRepository<Meal, Long> {
@Query(value ="select * from meal m where m.meal_type = ?1", nativeQuery = true)
List<Meal> findMealByType(MealType mealType);
Run Code Online (Sandbox Code Playgroud)
}
但是当我对其进行测试时,我不断得到 org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet
除此之外,我还尝试使用 MealType 作为参数重新编写查询:
@Query(value ="select * from meal m where m.meal_type in :meal_type ", nativeQuery = true)
List<Meal> findMealByType(@Param("meal_type") MealType mealType);
Run Code Online (Sandbox Code Playgroud)
但它引起了不同类型的错误
InvalidDataAccessResourceUsageException: could …
目前我有一个 axios post 请求,可以很好地将数据发送到 Spring Boot 后端。但它只是将单个数据列表包装到 json 并将其作为请求的正文发送:
例子:
sendAllData(data) {
return axios.post(API_URL + "receiveData", JSON.stringify(data),
{ headers: { "Content-Type": "application/json; charset=UTF-8" },
}).then(response=> console.log("repsonse", response.status)) // is 201
Run Code Online (Sandbox Code Playgroud)
}
它在后端接收它:
@RequestMapping(path = "/receiveData", method = RequestMethod.POST)
public ResponseEntity<Void> receiveData(@RequestBody Collection<ActivePOJO> activitePOJOS) {
//DO WORK WITH activePOJOS DATA
return new ResponseEntity<>(HttpStatus.CREATED);
}
Run Code Online (Sandbox Code Playgroud)
但是,除了这些信息,我还需要发送一些其他信息,如 user.id(例如),所以我需要我的后端接收这样的信息:
public ResponseEntity<Void> receiveData(@RequestBody Collection<ActivitePOJO> activePOJOS, Long userID)
Run Code Online (Sandbox Code Playgroud)
但我不知道我应该用哪种方式准备 axios 帖子。我应该使用 params 而不是 body 吗?