Javascript 在Redux ORM QuerySet上执行映射和筛选的问题
我正在为redux orm做教程,我需要在测试中的QuerySet实例上调用Javascript 在Redux ORM QuerySet上执行映射和筛选的问题,javascript,node.js,babeljs,jestjs,redux-orm,Javascript,Node.js,Babeljs,Jestjs,Redux Orm,我正在为redux orm做教程,我需要在测试中的QuerySet实例上调用map 回购协议中的原始测试为 这就是我如何创建Todo: const todoTags = 'testing,nice,cool' const user = session.User.first() const userId = user.getId() const action = { type: CREATE_TODO, payload: { text: todoText, tags: t
map
回购协议中的原始测试为
这就是我如何创建Todo
:
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)
我的代码如下所示:
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'])
标记
模型如下所示:
Tag.backend = {
idAttribute: 'name'
}
我可以使用
newTodo.tags.idArr
这是刻薄和不可接受的
测试失败,这是我的控制台输出
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
}
针对@markerikson的评论:
case CREATE_TODO:
const tags = payload.tags.split(',')
const trimmed = tags.map(tag => tag.trim())
trimmed.forEach(tag => Tag.create(tag))
break
在
标记中
模型处理减速机内的字符串。Todo
和Tag
的代码都是正如我在评论中所建议的:您没有正确创建标记实例。看起来您正在将每个单独的标记字符串直接传递给tag.create()
,因此它就像tag.create(“测试”)
。相反,您需要传递一个对象,比如Tag.create({name:“testing”})
正如我在评论中所建议的:您没有正确创建标记实例。看起来您正在将每个单独的标记字符串直接传递给tag.create()
,因此它就像tag.create(“测试”)
。相反,您需要传递一个对象,比如Tag.create({name:“testing”})
你是如何创建标签的?如果您试图将它们作为“just”字符串插入,则不会起作用。标记条目需要是模型本身。例如:{id:1,value:“testing”}
。你能链接你的整个代码吗,或者至少是一大块相关的代码吗?@markerikson我已经用模型更新了这个问题,标记的idAttribute是名称
属性。因此,我们可能需要执行`{name:'testing'}`另外,标记
reducer应该处理CREATE\u TODO
并拆分字符串。您是如何创建标记的?如果您试图将它们作为“just”字符串插入,则不会起作用。标记条目需要是模型本身。例如:{id:1,value:“testing”}
。你能链接你的整个代码吗,或者至少是一大块相关的代码吗?@markerikson我已经用模型更新了这个问题,标记的idAttribute是名称
属性。因此,我们可能需要执行`{name:'testing'}`另外,标记
reducer应该处理CREATE_TODO
并拆分字符串。