【发布时间】:2021-05-31 10:26:41
【问题描述】:
我正在尝试将教程改编为我自己的“照片制作应用程序”。但我不知何故难以将 SQLite 实施到我的项目中,我不明白错误以及为什么会得到它。
我有以下减速器:
import { ADD_PHOTO, SET_OBJECTS } from "./photo-actions";
import PhotoObject from "../models/photoObject";
const initialState = {
photos: [],
};
export default (state = initialState, action) => {
switch (action.type) {
case SET_OBJECTS:
return {
objects: action.objects.map(
(ob) => new Object(ob.id.toString(), ob.title, ob.imageUri)
),
};
case ADD_PHOTO:
const newObject = new PhotoObject(
action.photoData.id,
action.photoData.title,
action.photoData.image,
action.photoData.description
);
console.log("Ich bin das newObject aus photo-reducer.js");
console.log(newObject);
return {
photos: state.photos.concat(newObject), //<---------this is where I assume the error
};
default:
return state;
}
};
我的错误日志的最后一点如下所示:
Ich bin das newObject aus photo-reducer.js
PhotoObject {
"description": "cool",
"id": 1,
"imageUri": undefined,
"title": "Dog",
}
In photo-actions.js--> addPhoto wurde dispatch ausgeführt
ReferenceError: Can't find variable: state //<----- it says it can't find the state but I'm sure I defined it correctly, didn't I?
This error is located at:
in Gallery (created by SceneView)
in SceneView (created by CardContainer)
in RCTView (at View.js:34)
in View (created by CardContainer)
in RCTView (at View.js:34)
这是我的行动
...
export const addPhoto = (title, image, description) => {
return async (dispatch) => {
const fileName = image.split("/").pop();
const newPath = FileSystem.documentDirectory + fileName;
try {
await FileSystem.moveAsync({
from: image,
to: newPath,
});
const dbResult = await insertObject(
title,
newPath,
"dummyDescription aus photo-actions",
"locationTest aus photo-actions",
10
);
console.log("Ich bin das dbResult aus photo-actions.js-->addPhoto");
console.log(dbResult);
dispatch({
type: ADD_PHOTO,
photoData: {
id: dbResult.insertId,
title: title,
imageUri: newPath,
description: description,
},
});
console.log("In photo-actions.js--> addPhoto wurde dispatch ausgeführt");
} catch (err) {
console.log(err);
throw err;
}
};
};
export const loadObjects = () => {
return async (dispatch) => {
try {
const dbResult = await fetchObjects();
dispatch({ type: SET_OBJECTS, objects: dbResult.rows._array });
} catch (err) {
throw err;
}
};
};
这是我要在其中使用状态的组件:
...
const Gallery = (props) => {
const photos = useSelector((state) => state.photos.photos);
const dispatch = useDispatch();
useEffect(() => {
dispatch(objectActions.loadObjects());
}, [dispatch]);
return (
<FlatList
data={photos}
keyExtractor={(item) => item.id}
renderItem={(itemData) => (
...
问题是通过使用 expo ImagePicker 并希望存储获取的数据开始的:
...
const savePhotoHandler = () => {
dispatch(
photosActions.addPhoto(titleValue, selectedImage, descriptionValue)
);
props.navigation.navigate("Gallery");<--------------this command leads to the component where I receive the error when I want to grab the state
};
...
【问题讨论】:
-
根据您的日志,看起来错误发生在您认为的位置之后。看起来整个
addPhoto都正确通过。除了useSelector中的引用之外,Gallery屏幕上是否还有其他对state的引用? -
我在你的减速器中发现了一个错误,我会写一个答案,但我不确定它与特定的错误消息“找不到变量:状态”有何关系
标签: javascript reactjs react-native sqlite redux