无效的 MongoDB 连接字符串在数据库 URL 中。

huangapple go评论65阅读模式
英文:

Invalid MongoDB connection string in database URL

问题

"message": "\n无效的 `prisma.user.create()` 调用:\n\n\n提供的数据库字符串无效。MongoDB 连接字符串错误:数据库 URL 中的连接字符串方案无效:mongo。请参阅 https://www.prisma.io/docs/reference/database-reference/connection-urls 以构建正确的连接字符串。在某些情况下,必须转义某些字符。请检查字符串中是否有任何非法字符。",

// .env 文件
DATABASE_URL="mongodb+srv://<USER>:<PASS>@cluster0.<ID>.mongodb.net/nuxt-app"

// schema.prisma 文件
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id           String  @id @default(auto()) @map("_id") @db.ObjectId
  username     String  @unique
  password     String
}

// users.js 文件
import { prisma } from ".";

export const createUser = (userData) => {
    return prisma.user.create({
        data: userData
    })
}

// 使用的版本
"nuxt": "^3.2.3"
"prisma": "^4.11.0"
英文:

I am trying to write a Nuxt3 app using prisma and atlas MongoDB, I successfully set up all and npx prisma db push works fine, But when I send queries to your database I get the following error:

&quot;message&quot;: &quot;\nInvalid `prisma.user.create()` invocation:\n\n\nThe provided 
database string is invalid. MongoDB connection string error: invalid connection 
string scheme: mongo in database URL. Please refer to the documentation in 
https://www.prisma.io/docs/reference/database-reference/connection-urls for 
constructing a correct connection string. In some cases, certain characters must 
be escaped. Please check the string for any illegal characters.&quot;,

Files

// .env file
DATABASE_URL=&quot;mongodb+srv://&lt;USER&gt;:&lt;PASS&gt;@cluster0.&lt;ID&gt;.mongodb.net/nuxt-app&quot;
// schema.prisma
generator client {
  provider = &quot;prisma-client-js&quot;
}

datasource db {
  provider = &quot;mongodb&quot;
  url      = env(&quot;DATABASE_URL&quot;)
}

model User {
  id           String  @id @default(auto()) @map(&quot;_id&quot;) @db.ObjectId
  username     String  @unique
  password     String
}
// users.js
import { prisma } from &quot;.&quot;;

export const createUser = (userData) =&gt; {
    return prisma.user.create({
        data: UserData
    })
}

i am using

&quot;nuxt&quot;: &quot;^3.2.3&quot;
&quot;prisma&quot;: &quot;^4.11.0&quot;

答案1

得分: 0

数据库连接字符串中可能缺少集合名称:

DATABASE_URL="mongodb+srv://<user>:<pass>@<cluster_name>/<colection_name>?retryWrites=true&w=majority"
英文:

the string is probably missing a collection name

DATABASE_URL=&quot;mongodb+srv://&lt;user&gt;:&lt;pass&gt;@&lt;cluster_name&gt;/&lt;colection_name&gt;?retryWrites=true&amp;w=majority&quot;

huangapple
  • 本文由 发表于 2023年3月9日 19:04:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定