2017-10-17 1 views
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() 

答えて

1

をはい、すべてをラップする必要がありますtypes.optionalを使用し、デフォルトのスナップショットを提供します。 はここのシーンの後ろに行うtypes.optional何例

const MST = require("mobx-state-tree") 
const Order = require('./order') 

const Orders = MST.types.model({ 
    buys: MST.types.optional(MST.types.map(Order), {}), 
    sells: MST.types.optional(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() 

のはインターセプト未定義であり、あなたのデフォルト値:)

+0

[OK]を、おかげでそれを置き換えます。それは忘れてはならないピタのビットですが、運良くモデルは頻繁に変更されません。 – jimmy

関連する問題