'How do you return an array of string ids using a factory in MirageJS?
In MirageJS I am trying to create a factory for a simple array model.
Current code
Here is my code:
let server = new Server({
models: {
usertab: Model
},
factories: {
usertab: Factory.extend( function(i) { return i } ),
},
seeds(server) {
server.createList("tab", 3)
},
routes() {
this.get("api/usertabs", (schema) => {
return schema.usertabs.all()
})
}
})
Current result
The above code returns a list of objects with an id
key under the usertabs
key:
{
usertabs: [
{id: "1"},
{id: "2"},
{id: "3"},
]
}
Wanted Result
I want to seed the array with simple incrementing numbers so the return value when using GET api/usertabs
will return:
["1","2","3"]
For some reason there is no api documentation for Factory
only a guide. The only examples I could find are ones that create arrays of objects.
Solution 1:[1]
this.get("api/usertabs", (schema) => {
return schema.db.usertabs.map(item => item.id);
})
will return an array in stead of an object containing an array. it is described in more detail here: https://miragejs.com/docs/main-concepts/database/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |