2017-10-17 22 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

좋아, 감사와 그 대체합니다. 기억해야 할 피타의 비트이지만 운 좋은 모델은 자주 바뀌지 않습니다. – jimmy