英文:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'id') when trying to create
问题
I'm making a public prayer journal, where you can also add favorite Bible verses, in full stack. Whenever I try to add a prayer (or a verse) I get this error: "caught (in promise) TypeError: Cannot read properties of undefined (reading 'id')".
useEffect(() => {
getPrayers().then(setPrayer);
if (object.id) setFormInput(object);
}, [object]);
const handleChange = (e) => {
const { name, value } = e.target;
setFormInput((prevState) => ({
...prevState,
[name]: value,
}));
};
const handleSubmit = (e) => {
e.preventDefault();
if (object.id) {
updatePrayer(formInput, object.id)
.then(() => router.push('/prayers'));
} else {
const payload = { ...formInput };
createPrayer(payload).then(setFormInput(initialState));
}
};
I have tried changing user.id to user.uid, as well as change uid to user
const createPrayer = (prayerObj, user) => new Promise((resolve, reject) => {
const prayer = {
content: prayerObj.content,
publication_date: prayerObj.pub_date,
uid: user.id,
};
fetch(`${dbUrl}/prayers`, {
method: 'POST',
body: JSON.stringify(prayer),
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => resolve(response.json()))
.catch((error) => reject(error));
});
英文:
I'm making a public prayer journal, where you can also add favorite Bible verses, in full stack. Whenever I try to add a prayer (or a verse) I get this error: "caught (in promise) TypeError: Cannot read properties of undefined (reading 'id')".
useEffect(() => {
getPrayers().then(setPrayer);
if (object.id) setFormInput(object);
}, [object]);
const handleChange = (e) => {
const { name, value } = e.target;
setFormInput((prevState) => ({
...prevState,
[name]: value,
}));
};
const handleSubmit = (e) => {
e.preventDefault();
if (object.id) {
updatePrayer(formInput, object.id)
.then(() => router.push('/prayers'));
} else {
const payload = { ...formInput };
createPrayer(payload).then(setFormInput(initialState));
}
};
I have tried changing user.id to user.uid, as well as change uid to user
const createPrayer = (prayerObj, user) => new Promise((resolve, reject) => {
const prayer = {
content: prayerObj.content,
publication_date: prayerObj.pub_date,
uid: user.id,
};
fetch(`${dbUrl}/prayers`, {
method: 'POST',
body: JSON.stringify(prayer),
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => resolve(response.json()))
.catch((error) => reject(error));
});
答案1
得分: 1
以下是翻译好的内容:
最好展示错误的来源,但我认为错误出现在这个函数中:
const createPrayer = (prayerObj, user) => new Promise((resolve, reject) => {
const prayer = {
content: prayerObj.content,
publication_date: prayerObj.pub_date,
uid: user.id,
};
fetch(`${dbUrl}/prayers`, {
method: 'POST',
body: JSON.stringify(prayer),
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => resolve(response.json()))
.catch((error) => reject(error));
});
在这里,您设置了uid: user.id
,但是在createPrayer(payload).then(setFormInput(initialState));
中没有提供user
参数,因此user
未定义,这是您的错误。
为了更详细地解释这个错误,以便您下次理解(这是相当常见的错误):
TypeError: 当尝试创建对象时无法读取未定义的属性(读取'property')
在JavaScript中,对象的表示是{}
。您放在对象内部的任何内容都是属性,因此:
const obj = {
property: "value"
}
所以错误"无法读取未定义的属性(读取'property')"表示obj
未定义,因此无法获取property
的值。
英文:
Best to show where the error is coming from, however I assume it is this function:
const createPrayer = (prayerObj, user) => new Promise((resolve, reject) => {
const prayer = {
content: prayerObj.content,
publication_date: prayerObj.pub_date,
uid: user.id,
};
fetch(`${dbUrl}/prayers`, {
method: 'POST',
body: JSON.stringify(prayer),
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => resolve(response.json()))
.catch((error) => reject(error));
});
Here you are setting uid: user.id
, user is a param of the function but is not supplied here createPrayer(payload).then(setFormInput(initialState));
Hence user is undefined, which is your error.
To go into a bit more detail so you understand the error next time (it is fairly common)
TypeError: Cannot read properties of undefined (reading 'property') when trying to create
an object in js is {}. Anything you put inside an object is a property so:
const obj = {
property: "value"
}
so the error cannot read properties of undefined (reading property) is saying obj is not defined so I cannot get the value of property.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论