解构时,我不断得到public ClientResponse runAdHoc(String sql, Object... sqlArgs) throws IOException, ProcCallException
{
ClientConfig clientConfig = new ClientConfig();
Client voltdbClient = ClientFactory.createClient(clientConfig);
voltdbClient.createConnection(voltdbServer, voltdbPort);
Object[] procArgs;
if (sqlArgs == null || sqlArgs.length == 0)
{
procArgs = new Object[1];
} else
{
procArgs = new Object[sqlArgs.length + 1];
System.arraycopy(sqlArgs, 0, procArgs, 1, sqlArgs.length);
}
procArgs[0] = sql;
return voltdbClient.callProcedure("@AdHoc", procArgs);
}
payload
:
undefined
如何检查未定义?我已经尝试过以下检查,但仍会收到错误消息:
let videosArray = [];
if (payload.videos) {
const {
payload: { videos }
} = action;
videosArray = videos;
}
return videosArray;
答案 0 :(得分:0)
您需要定义这样的操作const action = {payload: {videos: ["Matrix", "Star Wars"]}}
您可以像这样if(action && action.payload)
检查有效载荷是否未定义,然后可以执行销毁操作。
答案 1 :(得分:0)
您只需要确保先定义动作,然后访问其参数即可,而不是自己调用有效负载。在您的示例中,当您尝试访问payload
时似乎没有声明
function getVideos(action) {
if (action && action.payload && action.payload.videos) {
const {payload: {videos}} = action;
return videos;
}
return [];
}
console.log(getVideos()); // action undefined, expected []
console.log(getVideos({})); // payload undefined, expected []
console.log(getVideos({payload: {}})); // videos undefined, expected []
console.log(getVideos({payload: {videos: [1,2,3]}})); // expected [1,2,3]
当然,如果您确实想通过分解来完成任务,则可以简单地创建一些默认值,如下所示:
function getVideos(action) {
const {payload: {videos=[]}={}} = action || {};
return videos;
}
console.log(getVideos()); // action undefined, expected []
console.log(getVideos({})); // payload undefined, expected []
console.log(getVideos({payload: {}})); // videos undefined, expected []
console.log(getVideos({payload: {videos: [1,2,3]}})); // expected [1,2,3]
答案 2 :(得分:0)
您可以在不破坏结构的情况下进行检查并获得默认值。
function getVideos(action) {
return action && action.payload && action.payload.videos || [];
}
console.log(getVideos()); // action undefined, expected []
console.log(getVideos({})); // payload undefined, expected []
console.log(getVideos({ payload: {} })); // videos undefined, expected []
console.log(getVideos({ payload: { videos: [1, 2, 3] } })); // expected [1, 2, 3]