2

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: "demo@gmail.com"
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.

Rajat Sharma
  • 65
  • 1
  • 8

2 Answers2

0

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)
Abbas Hussain
  • 1,277
  • 6
  • 14
  • I’ll try to check later, but can you please specify the reason? Why it was throwing error? bcz I have been following the same scenario from so long, never faced that issue. @Abbas – Rajat Sharma Mar 29 '22 at 06:41
  • can you show the error what you are getting ? – Abbas Hussain Mar 29 '22 at 07:05
  • 1
    I tried your way of updating as well, still getting same error. I already mentioned the error in the title. Here is the error ```Cannot assign to read only property 'approved_by_customer_admin' of object '#'``` @Abbas – Rajat Sharma Mar 29 '22 at 14:29
  • can you edit the question to the full code what and where are you try to doing this beacause i try your function in google chrome console and it's also works – Abbas Hussain Mar 29 '22 at 14:59
  • That's exactly i was concerned about, I tried the same in google chrome console and it was working fine, but not working in my react code. Let me try to explain a bit more -> I called getUser api from my reactJS (backend - Nodejs and MongoDB), and displayed all the users in a list(in UI), for each user there is accept button to approve the user, so on click of btn, I need to set the ```approved_by_customer_admin``` to **true**. So, I am calling the func. which I have mentioned in ques. and I also mentioned the full user obj. Let me update the ques. a little bit more detailed. – Rajat Sharma Mar 29 '22 at 15:08
  • I have updated my code now, please have a look and try to solve it. If you need more code then let me know. @Abbas – Rajat Sharma Mar 29 '22 at 15:26
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)

Fred Klein
  • 448
  • 6
  • 13