mongoose 数据对象属性为只读

m528fe3b  于 2023-06-23  发布在  Go
关注(0)|答案(1)|浏览(108)

我知道,有时候当前端从mongoose接收数据时,它是作为只读文档。然而,当我今天去的错误:TypeError:“contracts”是只读的,我的第一个想法是这样的,所以我给数据分配了一个新的变量,尝试在数据上运行一个Map(data.map(x => x)),尝试用一个spread操作符分配数据,但我总是得到相同的错误,所以我真的不知道我错过了什么。
以下是控制台中记录的数据:

Array [ {…}, {…} ]
​
0: Object { _id: "6205a8313fe12d6b4ec354c4", fullName: "Johnny Cochrane", contracts: {…} }
​​
_id: "6205a8313fe12d6b4ec354c4"
​​
contracts: Object { type: "Quarterly", hours: 120 }
​​
fullName: "Johnny Cochrane"
​​
<prototype>: Object { … }
​
1: Object { _id: "6217c1b73fe12d6b4ec3550e", fullName: "Sheila Thompson", contracts: {…} }

这里是我分配抛出错误的新字段的地方:

useEffect(() => {
        if(data) {

            let dataArr = [...data]
        
            const contractsByMonth = (type, hours) => {
                if (type === "Quarterly") {
                let monthHours = hours / 3
                return +(Math.round(monthHours + "e+2")  + "e-2");
            }     
            }

            dataArr.map(i => {
                let monthly = contractsByMonth(i.contracts.type, i.contracts.hours)
                i.contracts = {type: i.contracts.type, monthHours: monthly, total: i.contracts.hours  } 

...
}, [data])

所以我知道数据就在那里,它不是在需要解析的字符串中,甚至试图重新分配它似乎也不起作用。欣赏任何人的想法。应该不是问题,但我使用RTK查询来获取数据,但我一直使用它没有这个问题,我猜它将数据视为原始mongoose返回的文档。

编辑

添加更多组件代码:

const HoursGraph = () => {
 const {id} = useAuth()
 const userId = id

...

const  [pullData, {data}]  = useGetSnapshotMutation({userId, isStart, isEnd}) 
// RTK Query mutation which pulls the data- its an aggregation and I pass criteria through the body, but I've done this plenty without issue

...
useEffect(() => {
 ...
if (index === 0) {
            start = new Date(end).setDate(1)
        } else if (index === 1) {
            const startMth = currentMth - 1
            console.log(currentMth, startMth)
            start = new Date(new Date(new Date(end).setMonth(startMth, 1)).setHours(0, 0, 0)).toISOString()
        } else if (index === 2) {
            const startMth = currentMth -2
            console.log(currentMth, startMth)
            start = new Date(new Date(new Date(end).setMonth(startMth, 1)).setHours(0, 0, 0)).toISOString()
        }
        setIsStart(start)
        setIsEnd(end)
}, [])

 useEffect(() => {
        if(isStart && isEnd && userId) {
            pullData({userId, isStart, isEnd})
        }
    }, [isStart, isEnd, userId])
    
    console.log("snapshot data: ", data) //this logs the data referenced above

 useEffect(() => {
        if(data) {

            let dataArr = [...data]
        
            const contractsByMonth = (type, hours) => {
                if (type === "Quarterly") {
                let monthHours = hours / 3
                return +(Math.round(monthHours + "e+2")  + "e-2");
            }     
            }

            dataArr.map(i => {
                let monthly = contractsByMonth(i.contracts.type, i.contracts.hours)
                i.contracts = {type: i.contracts.type, monthHours: monthly, total: i.contracts.hours  } //This throws the error

                let colors
                let sum

                let qtrSum = i.hours.reduce((acc, curr) => acc.count + curr.count)
                console.log("sum: ", qtrSum)
                const color= ['#73E078', '#26A69A', '#26C6DA', '#29B6F6']
                
                i.hours = i.hours.map(x => {return ( {date: x.date, count: x.count, month: monthly } )})
                i[colors] = color
                i[sum] = qtrSum
                })
                console.log(data)
...
}, [data])
svmlkihl

svmlkihl1#

与其分配给i.contracts(它看起来是只读的),不如尝试

dataArr.map((i) => {
  const monthly = /*yourt code here*/
  const contracts = {...i.contracts, monthHours: monthly, total: i.contracts.hours} //this obviously has a different type than the original i.contracts, which is an issue you will still have to deal with
  return {...i, contracts};
})

这个问题可能是你试图在i上改变的合约类型是只读的(而你试图将它改变为不同的类型)。RTK Query在后台使用immer,如果您在端点的transformResponse字段中进行编辑,这可能会保护您免受重写该属性的后果。但它可能不允许您在过程中更改类型。

相关问题