0
我試圖用mobx-state-tree創建一個超級簡單的嵌套存儲,我無法弄清楚如何讓它工作。要麼這個圖書館非常不直觀,要麼我錯過了一些顯而易見的東西。我嘗試在MST.types.optional()
中包裝所有內容,看看這是否有所作爲,但不是。將undefined轉換爲map <string,AnonymousModel>時出錯
這個想法是,訂單商店有很多買賣訂單。我想創建一個沒有任何訂單的空商店。
當我嘗試執行Orders.js,我得到以下錯誤:
Error: [mobx-state-tree] Error while converting `undefined` to `map<string, AnonymousModel>`: value `undefined` is not assignable to type: `map<string, AnonymousModel>` (Value is not a plain object), expected an instance of `map<string, AnonymousModel>` or a snapshot like `Map<string, { timestamp: Date; amount: number; price: number }>` instead.`
order.js
const MST = require("mobx-state-tree")
const Order = MST.types.model({
timestamp: MST.types.Date,
amount: MST.types.number,
price: MST.types.number,
}).actions(self => {
function add(timestamp, price, amount) {
self.timestamp = timestamp
self.price = price
self.amount = amount
}
return { add }
})
module.exports = Order
orders.js
const MST = require("mobx-state-tree")
const Order = require('./order')
const Orders = MST.types.model({
buys: MST.types.map(Order),
sells: MST.types.map(Order),
}).actions(self => {
function addOrder(type, timestamp, price, amount) {
if(type === 'buy'){
self.buys.add(timestamp, price, amount)
} else if(type === 'sell') {
self.sells.add(timestamp, price, amount)
} else throw Error('bad order type')
}
return { addOrder }
})
Orders.create()
好的,謝謝。要記住要做點皮塔餅,但幸運的是,模特經常不會改變。 – jimmy