最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Why do I get a D1_type_error on insert? - Stack Overflow

programmeradmin2浏览0评论

I have a contact form but want a blob type. When I try to insert into the database I get:

D1_type_error: Type object is not supported for value [object Object]

server/database/schema.ts:

import { sqliteTable, integer, blob } from "drizzle-orm/sqlite-core"
import type { TranslatedText } from "../utils/types"

export const products = sqliteTable("products", {
  id: integer("id").primaryKey({autoIncrement: true}),
  description: blob("description").$type<TranslatedText>()
})

server/utils/types.ts:

export type TranslatedText = {
  en: string
  fr: string
}

app/pages/test.vue:

<script setup lang="ts">
const newProduct: {
  description: {
    en: "Hello world",
    fr: "Bonjour le monde"
  }
}

const createdProduct = await $fetch("/api/test/product/edit", {
  method: "POST",
  body: newProduct
})

console.log(createdProduct);
</script>

/server/api/test/product/edit.post.ts:

export default eventHandler(async (event) => {
  const obj = await readBody(event)
  // const wrappedObj = [obj]

  if (!obj.id || obj.id <= 0) {
    // create new
    try {
      const result = await useDrizzle()
        .insert(tables.products)
        .values(obj)
      return result
    } catch (err) {
      console.log("error :")
      console.log(err)
    }
  }

  // Edit existing product
  // ...

})

Migrations are OK, table is created with the right type. Other inserts are working. I tried to wrap the object in an array but no luck.

发布评论

评论列表(0)

  1. 暂无评论