AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[mongoose](coding)

Martin Hope
Turing Vang
Asked: 2024-11-27 19:30:22 +0800 CST

Nextjs 动作,如何从动作中进行 mongoose 调用?

  • 5

应该这样做吗?或者 find 的实际 mongoose 调用是否应该作为 api 放在 route.ts 文件中,并在 nextjs 的操作中对这个 api 进行 http fetch 调用?

我的问题主要是:

  • 我应该在(服务器端)操作内执行 mongoose find 命令吗
  • 如果是这样,如何转换为 json,因为在常规代码中我们将使用 nextResponse.json 将 mongoose 转换为 json

谢谢

mongoose
  • 1 个回答
  • 12 Views
Martin Hope
VARUN KHARKWAL
Asked: 2023-09-17 21:50:34 +0800 CST

使用猫鼬时如何在模式中使用“匹配”

  • 6

我正在通过 YouTube 视频Next.js 13 Full Course 2023学习 Next.js ,在这里他讲述了“匹配”,他将其用作数据的要求,以通过在其中添加一些代码来匹配他给出的一些属性。

username: {
        type: String,
        required : [true, 'username is required'],
        match: [/^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/, "Username invalid, it should contain 8-20 alphanumeric letters and be unique!"], 
    },

我想更多地了解这种编写匹配的方式以及如何更改或制作自己的匹配,但我还找不到任何文档或解释。请帮助我学习如何使用它。我目前使用 Nextjs 与 MongoDB 作为数据库和 Mongoose。如果需要,这是我的 pakage.json 的副本。

{
  "name": "surveysnap",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "autoprefixer": "10.4.15",
    "express": "^4.18.2",
    "mongoose": "^7.5.2",
    "next": "13.4.19",
    "next-auth": "^4.23.1",
    "postcss": "8.4.28",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-icons": "^4.10.1",
    "tailwindcss": "3.3.3"
  }
}

我使用的架构的完整代码是这样的。

import { Schema, model, models } from "mongoose";
// models is the collection of all the model that is associated with the mongoose library
const UserSchema = new Schema({
    email: {
        type : String,
        unique : [true, 'email already in use'],
        required : [true, 'email already in use'],
    },
    username: {
        type: String,
        required : [true, 'username is required'],
        match: [/^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/, "Username invalid, it should contain 8-20 alphanumeric letters and be unique!"], 
    },
    password: {
        type: String,
        match: [/^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/, "Password invalid, it should contain 8-20 alphanumeric letters and be unique!"],
    },
    image: {
        type: String,
    },
    membership: {
        type: String,
        required : [true, 'membership is information is required'],
    },
    survey_created: {
        type: Number,
    },
    survey_answered: {
        type: Number,
    },
    snap_points: {
        type: Number,
    },

});

const User = models.User || model("User", UserSchema);
// if a "User" already exists, models assign the existing one to avoid redefining model and ensuring the existing one be used.
// Otherwise it will create a new one.
export default User;

关于如何在架构中使用匹配的说明。

mongoose
  • 1 个回答
  • 11 Views
Martin Hope
Manu Chadha
Asked: 2023-09-03 19:06:01 +0800 CST

关于猫鼬文档的清晰度 - 连接返回连接数组

  • 7

我正在使用mongoose并参考api https://mongoosejs.com/docs/api/mongoose.html

我这么package.json说,"mongoose": "^7.4.0"所以我不确定我使用的是哪个特定版本。假设是7.5.0,我正在使用connectcall 来连接到mongodbviamongoose

const connection = await mongoose.connect(process.env.DATABASE_URL);
          var localdbClient = connection.connections[0].client;

我在文档中找不到它说connection有的地方connections array。哪里有提到呢?类型是connection[]a吗connection?这并不是这么说的 - https://mongoosejs.com/docs/api/mongoose.html

我应该如何引用api和definitions?

mongoose
  • 1 个回答
  • 8 Views
Martin Hope
Manu Chadha
Asked: 2023-08-19 00:37:34 +0800 CST

如何关闭所有数据库连接 - Moongose

  • 5

似乎当我连接到mongodb使用connect apiof时,我在对象mongoose内得到了一个连接数组connection

const connection = await mongoose.connect(process.env.DATABASE_URL);
        var dbClient = connection.connections[0].client;

如果我想关闭连接,我是否需要遍历每个连接connection,或者是否有更高级别的 api 可以关闭所有连接?

mongoose
  • 1 个回答
  • 8 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve