2017-01-06 3 views
0

redux-ormのチュートリアルを行っていますhere私のテスト内でQuerySetインスタンスにmapを呼び出す必要があります。Redux ORMでマップとフィルタを実行する問題QuerySet

レポでオリジナルのテストがhere

これは私がTodoを作成する方法である:Tagモデルがどのように見える

const newTodo = Todo.last() 
console.log(newTodo.tags.forEach, newTodo.tags.map) 
console.log('Print all tags') 
newTodo.tags.forEach(tag => { 
    console.log('Prints a tag') 
    console.log(tag) 
}) 

newTodo.tags.map(tag => { 
    console.log('Prints a tag 2') 
    console.log(tag) 
    return tag 
}) 

expect(newTodo.text).toEqual(todoText) 
expect(newTodo.user.getId()).toEqual(userId) 
expect(newTodo.done).toBeFalsy() 
const newTodoTags = newTodo.tags.map(tag => tag.name) 
console.log(newTodoTags) 
expect(newTodoTags).toEqual(['testing','nice','cool']) 

const todoTags = 'testing,nice,cool' 
const user = session.User.first() 
const userId = user.getId() 

const action = { 
    type: CREATE_TODO, 
    payload: { 
    text: todoText, 
    tags: todoTags, 
    user: userId 
    } 
} 

const { Todo, Tag, User } = applyActionAndGetNextSession(schema, state, action) 

私のコードは次のようになります。

Tag.backend = { 
    idAttribute: 'name' 
} 

私はハックと受け入れられない

newTodo.tags.idArr 

を使用して、このモデルのためにidsことが起こるの名前を取得することができます。

テストが失敗すると、これは@markeriksonコメントに応えて

console.log(newTodo.tags) 

//OUTPUT 
QuerySet { 
    ... 
    idArr: ['testing', 'nice', 'cool'] 
    ... 
} 

console.log(newTodo.tags.forEach, newTodo.tags.map) 

//OUTPUT 
[ Function forEach] [Function map] 

console.log(newTodo.tags.toRefArray()) 

//OUTPUT 
[undefined, undefined, undefined] 

console.log('Print all tags') 
newTodo.tags.forEach(tag => { 
    console.log('Prints a tag') 
    console.log(tag) 
}) 

newTodo.tags.map(tag => { 
    console.log('Prints a tag 2') 
    console.log(tag) 
    return tag 
}) 

//OUTPUT 
Prints all tags 

console.log(newTodo.tags.withModels) 

//Output is a QuerySet 

newTodo.tags.withModels.map(tag => { 
    console.log('mapping over tag models') 
    console.log(tag) 
    return tag 
} 

のための私のコンソール出力です:

case CREATE_TODO: 
    const tags = payload.tags.split(',') 
    const trimmed = tags.map(tag => tag.trim()) 
    trimmed.forEach(tag => Tag.create(tag)) 
    break 

Tagモデルでは、減速中の文字列を処理します。 TodoTagのコードは、here

+0

どのようにタグを作成していますか?それらを「ちょうど」の文字列として挿入しようとすると、それは動作しません。タグエントリはモデル自体である必要があります。例は '{id:1、value:" testing "}'のようになります。あなたはあなたのコード全体、または少なくともそれに関連する大きなチャンクをリンクできますか? – markerikson

+0

@markerikson私はモデルで質問を更新しました。 'Tag'のidAttributeは' name'プロパティです。 '{name: 'testing'}'を実行する必要があるかもしれません。また、 'Tag'リデューサは' CREATE_TODO'を処理し、文字列を分割する必要があります。 – vamsiampolu

答えて

1

です。私がコメントしたところでは、タグインスタンスを正しく作成していません。個々のタグ文字列をTag.create()に直接渡しているようです。Tag.create("testing")のようになります。代わりにTag.create({name : "testing"})のようにオブジェクトを渡す必要があります。

関連する問題