无法分配仅读取属性'批准_by_customer_admin'对象'#< object>'手动更新API数据后
我知道之前已经问过同样的问题,但它们都不为我工作,所以这是我的要求 - > 我有用户列表,我登录为管理员,首先我叫getusers
api获取用户列表并在UI中显示列表,有一个按钮 Accept 让每个用户批准它们,因此单击接受按钮,我称此方法 - >
const [allUserList, setAllUserList] = useState(usersArray)
const [filterUserListAfterUpdate, setFilterUserListAfterUpdate] = useState([])
const onClickAcceptUser (id) => {
let updatedUserList = []
allUserList.forEach(user => {
if(user._id === id){
user.approved_by_customer_admin = true
}
updatedUserList.push(user)
})
setFilterUserListAfterUpdate(updatedUserList)
}
return(<UserList filteredUsersList = {filterUserListAfterUpdate} onAccept={(id) => onClickAcceptUser(id) />)
note - :我正在使用nodejs,如后端和mongodb,这是我的完整用户对象=&gt;
//All the below values are dummy not real.
approved_by_customer_admin: false
auth0_id: "email|622e0414804c"
company_id: "622df44843fc4"
created_date: "2022-03-13T14:47:52.589Z"
email: "[email protected]"
industry_name: "gmail"
is_active: false
phone_number: ""
prefer_contact: "email"
role: "customer_auditor"
updated_date: "2022-03-13T14:47:52.589Z"
__v: 0
_id: "6243ffa"
我只需要更改对象的一个属性(或者将来可能多个)。 在Line user.appraved_by_customer_admin = true
我在控制台中遇到了错误。 任何建议都是可观的。
I know that the same question has been asked before but none of them are working for me, so here is my requirement ->
I have users list and I logged in as a Admin, First I called getUsers
api to get the list of the users and displayed the list in the UI, there is a button Accept for each user to approve them, So on click of Accept button, I am calling this method ->
const [allUserList, setAllUserList] = useState(usersArray)
const [filterUserListAfterUpdate, setFilterUserListAfterUpdate] = useState([])
const onClickAcceptUser (id) => {
let updatedUserList = []
allUserList.forEach(user => {
if(user._id === id){
user.approved_by_customer_admin = true
}
updatedUserList.push(user)
})
setFilterUserListAfterUpdate(updatedUserList)
}
return(<UserList filteredUsersList = {filterUserListAfterUpdate} onAccept={(id) => onClickAcceptUser(id) />)
NOTE -: I am using NodeJs as in backend and MongoDB and this is my full user object =>
//All the below values are dummy not real.
approved_by_customer_admin: false
auth0_id: "email|622e0414804c"
company_id: "622df44843fc4"
created_date: "2022-03-13T14:47:52.589Z"
email: "[email protected]"
industry_name: "gmail"
is_active: false
phone_number: ""
prefer_contact: "email"
role: "customer_auditor"
updated_date: "2022-03-13T14:47:52.589Z"
__v: 0
_id: "6243ffa"
I need to change only one property of object(or maybe more than one, in future).
At the line user.approved_by_customer_admin = true
I got the error in my console.
Any suggestions would be appreciable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在黑暗中敲击:根据您在提到“无法分配给只读属性”的答案之一中的评论,我怀疑像 Mongoose 这样的其他组件会产生错误。
一些搜索引导我如何修复错误(TypeError:无法分配给对象“#”的只读属性“map”) 和 https://github.com/Automattic/mongoose/issues/11377,建议降级Node (严格低于17.5.0)
Tapping in the dark: based in your comment in one of the answer mentioning "Cannot assign to read only property" I am suspecting some other component like Mongoose to produce the error.
Some search lead me to How to fix the error (TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>') and https://github.com/Automattic/mongoose/issues/11377, suggesting to downgrade Node (strictly below 17.5.0)
找到对象数组的索引并像这样更新它的值
find an index of array of object and update the value of that like this