英文:
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:
"message": "\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.",
Files
// .env file
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
})
}
i am using
"nuxt": "^3.2.3"
"prisma": "^4.11.0"
答案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="mongodb+srv://<user>:<pass>@<cluster_name>/<colection_name>?retryWrites=true&w=majority"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论