小编Tia*_*dão的帖子

如何使用 Typescript 在 mongoose 中正确输入对象 ID 数组

我有一个帖子模型:

const PostSchema = new Schema<IPost>(
  {
    // ...
    likes: [{ type: Schema.Types.ObjectId, ref: "User" }],
    // ...
  }
)

export default model<IPost>("Post", PostSchema)
Run Code Online (Sandbox Code Playgroud)
export interface IPost {
  // ...
  likes: ObjectId[]
  // ...
}

export interface IPostDocument extends Document, IPost {}
Run Code Online (Sandbox Code Playgroud)

我正在尝试切换用户,例如:

export const toggleLike: TController = async (req, res, next) => {
  const user = req.user as IUserDocument;
  const userId = user._id;
  const postId = req.params.postId;
  try {
    const disliked = await PostModel.findOneAndUpdate(
      { _id: postId, likes: userId …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb typescript

7
推荐指数
2
解决办法
2934
查看次数

如何从 Jest 中的超级测试请求中检索 cookie?

我刚刚开始使用 Jest,正在尝试测试端点POST/register。该端点发回 2 个 cookie:accessToken:*jwt*refreshToken:*jwt*

这是我的测试文件:

import server from "../server"
import supertest from "supertest"
import mongoose from "mongoose"

import dotenv from "dotenv"
dotenv.config()

const request = supertest(server)

describe("Testing Auth endpoints", () => {
  beforeAll(done => {
    const PORT = process.env.PORT
    mongoose
      .connect(process.env.MONGO_STRING_TEST!)
      .then(() => {
        console.log("Connect to Atlas. Test DB.")
        done()
      })
      .catch(err => console.log(err))
  })

  const validUser = {
    name: "Paul Stevens",
    email: "ps@g.com",
    password: "1234",
    role: "host",
  }

  it("should test /register for …
Run Code Online (Sandbox Code Playgroud)

cookies typescript supertest jestjs

4
推荐指数
1
解决办法
3288
查看次数

标签 统计

typescript ×2

cookies ×1

jestjs ×1

mongodb ×1

mongoose ×1

supertest ×1