[英]Need help sending correct payload to update State
I am using MongoDB-Stitch application to create a phonebook app.我正在使用 MongoDB-Stitch 应用程序来创建电话簿应用程序。 When I update a user the information gets saved correctly in the database but it won't show on the client until after I refresh the page.
当我更新用户时,信息会正确保存在数据库中,但在我刷新页面之前它不会显示在客户端上。 I believe the issue is in my reducer with the action.payload._id but I'm not really sure if that is it.
我相信问题出在我的减速器中, action.payload._id但我不确定是否是这样。 Reducer
减速器
case 'updateContact':
return {
...state,
contacts: state.contacts.map((contact) =>
contact._id === action.payload._id? action.payload : contact
),
loading: false,
};```
Action
```const updateContact = async (contactId, contact) => {
const query = { _id: contactId };
const update = {
$set: {
contact,
},
};
const options = { upsert: false };
await items.updateOne(query, update, options);
dispatch({
type: 'updateContact',
payload: [contact, contactId],
});
};
My data is stored like this:我的数据是这样存储的:
contacts: [
{
"_id":"5eb0c9238016c9de09f3d307",
"contact":{
"name":"Anne Bonny",
"email":"anne@bonny.com",
"phone":"3213231423"
},
"owner_id":"5ea89a7e94861451f4c4fe6f"
},
{
"_id":"5eb0c93f8016c9de09f3d308",
"contact":{
"name":"Woodes Rogers",
"email":"woodes@rogers.com",
"phone":"3217037475"
},
"owner_id":"5ea89a7e94861451f4c4fe6f"
},
{
"contact":{
"name":"john silver",
"email":"longjohn@silver.com",
"phone":"9391032314"
},
"owner_id":"5ea89a7e94861451f4c4fe6f",
"_id":"5eb19220a6949dfb5c76e30b"
},
{
"contact":{
"name":"Charles Vane",
"email":"charles@vane.com",
"phone":"3921303921"
},
"owner_id":"5ea89a7e94861451f4c4fe6f",
"_id":"5eb19234a6949dfb5c76e30c"
}
]```
You have sent payload as an array with contactId
and contact
to reducer, where you are expecting the payload to have _id
field in reducer.您已将有效负载作为带有
contactId
和contact
的数组发送到reducer,您希望有效负载在reducer 中有_id
字段。 You probably just need to send the contact and use the _id
field from it assuming the contact sent to update contact is of the format您可能只需要发送联系人并使用其中的
_id
字段,假设发送到更新联系人的联系人的格式为
{
"_id":"5eb0c93f8016c9de09f3d308",
"contact":{
"name":"Woodes Rogers",
"email":"woodes@rogers.com",
"phone":"3217037475"
},
"owner_id":"5ea89a7e94861451f4c4fe6f"
},
Change your action to below code to make it work将您的操作更改为以下代码以使其正常工作
const updateContact = async (contactId, contact) => {
const query = { _id: contactId };
const update = {
$set: {
contact,
},
};
const options = { upsert: false };
await items.updateOne(query, update, options);
dispatch({
type: 'updateContact',
payload: contact,
});
};
In case your contact object is simply of the below format如果您的联系人 object 只是以下格式
{
"name":"Woodes Rogers",
"email":"woodes@rogers.com",
"phone":"3217037475"
},
You would need to pass on the contactId separately and update your reducer too like below您需要单独传递contactId并更新您的reducer,如下所示
const updateContact = async (contactId, contact) => {
const query = { _id: contactId };
const update = {
$set: {
contact,
},
};
const options = { upsert: false };
await items.updateOne(query, update, options);
dispatch({
type: 'updateContact',
payload: { contact, contactId },
});
};
and reducer will be as follows和减速器将如下
case 'updateContact':
return {
...state,
contacts: state.contacts.map((contact) =>
contact._id === action.payload.contactId ? { ...contact, contact: action.payload.contact } : contact
),
loading: false,
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.