You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use mswjs/data with mswjs/http-middleware with this code:
db.js
import { faker } from '@faker-js/faker'
import { factory, primaryKey, oneOf } from '@mswjs/data'
faker.seed(123)
function createItem(db) {
return db.book.create({
author: db.author.create(),
})
}
const db = factory({
book: {
id: primaryKey(() => faker.datatype.uuid()),
title: faker.lorem.text,
year: faker.date.past().getFullYear(),
author: oneOf('author')
},
author: {
id: primaryKey(() => faker.datatype.uuid()),
name: faker.name.fullName,
},
})
for(let k = 0; k < 100; k++){
createItem(db)
}
export default db
server.js
import express from 'express'
import { createMiddleware } from '@mswjs/http-middleware'
import db from "./db.js";
const app = express()
app.use(express.json())
app.use(createMiddleware(...db.book.toHandlers('rest'), ...db.author.toHandlers('rest')))
app.use((_req, res) => {
res.status(404).send({ error: 'Mock not found' })
})
app.listen(process.argv[2],() => {
console.log(`Mock server started on port ${process.argv[2]}`)
})
I start the server with
node ./server.js 9090
almost everything works fine but when I try to create a new book with a POST to localhost:9090/books and a JSON payload like:
{"message":"Failed to resolve a \"ONE_OF\" relationship to \"author\" at \"book.author\" (id: \"861737c5-7ecd-40a8-be99-634b46bad09a\"): expected a referenced entity to be \"author\" but got {\"id\":\"b463b8bb-76cf-46a9-b266-5ab5730b69ba\",\"name\":\"Ms. Bessie Daniel\"}"}
I verified that the author was correct and if I try to create a book without the author, the book it's correctly saved.
I tried different payloads with Postman, but without luck.
Hey, @mauroaccornero. I'm sorry to hear you're having trouble making http-middleware and data work together. Do you happen to have your project published on GitHub? If you create a reproduction repository/sandbox I can help you look into the issue.
Hello,
I'm trying to use mswjs/data with mswjs/http-middleware with this code:
db.js
server.js
I start the server with
almost everything works fine but when I try to create a new book with a POST to localhost:9090/books and a JSON payload like:
I get a 500 status code with this message:
I verified that the author was correct and if I try to create a book without the author, the book it's correctly saved.
I tried different payloads with Postman, but without luck.
I'm using
with node v16.14.0
probably I'm missing something, any suggestion it's welcome.
update: Looks like the error comes from msw/data https://github.com/mswjs/data/blob/main/src/relations/Relation.ts#L172
The text was updated successfully, but these errors were encountered: