无法分配仅读取属性'批准_by_customer_admin'对象'#< object>'手动更新API数据后

发布于 2025-01-17 11:57:20 字数 1569 浏览 0 评论 0原文

我知道之前已经问过同样的问题,但它们都不为我工作,所以这是我的要求 - > 我有用户列表,我登录为管理员,首先我叫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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2025-01-24 11:57:21

在黑暗中敲击:根据您在提到“无法分配给只读属性”的答案之一中的评论,我怀疑像 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)

滿滿的愛 2025-01-24 11:57:20

找到对象数组的索引并像这样更新它的值

let updatedUserList = [...allUserList]
const objIndex = updatedUserList.findIndex(user => user._id == approveUser.id);
updatedUserList[objIndex].approved_by_customer_admin = true;
filterUserListAfterUpdate(updatedUserList)

find an index of array of object and update the value of that like this

let updatedUserList = [...allUserList]
const objIndex = updatedUserList.findIndex(user => user._id == approveUser.id);
updatedUserList[objIndex].approved_by_customer_admin = true;
filterUserListAfterUpdate(updatedUserList)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文