簡體   English   中英

Typescript 和 mongoose:保存到數據庫

[英]Typescript and mongoose: saving to database

我將以下應用程序保存到 memory 存儲庫中。 我連接了本地 mongodb,但將發布的數據保存到 mongodb 時遇到問題。 當應用程序保存到 memory 時,它工作得很好,我可以使用 curl 來顯示保存在那里的不同事件的數組。 我現在想讓它保存到我的數據庫中,這樣我就可以使用保存的數據,但我找不到任何明確的教程有人可以建議應該怎么做嗎?
Mongodb 架構:

import { mongoose } from '././http'

const CompetitionSchema = new Schema({
  id: String,
  place: String,
  time: String,
  subscriptions: [],
  date: new Date(),
  cost: {
    currency: String,
    amount: Number,
  },
})

export const CompetitionModel = mongoose.model(
  'CompetitionModel',
  CompetitionSchema,
)

export default CompetitionSchema

http:

export const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
const db = mongoose.connection
db.on('error', console.error.bind(console, 'An error has occured: '))
db.once('open', function () {
  console.log('Connected to Mongodb')
})

const express = require('express')
const app = express()
const bodyParser = require('body-parser')

app.use(bodyParser.json())
app.get('/events', (_req: any, res: any) => {
  res.send(eventApplication.getAll())
})

app.post('/event', async (req: any, res: any) => {
  await eventApplication.createAnEvent(req.body)
  res.json({
    success: true,
  })
})

app.listen(8000)

在 memory 和 mongodb 存儲庫中


export interface EventRepositoryInterface {
  // will require ansyc call will have return promise of all below - refactor needed
  save: (event: CompetitionEvent) => void
  getById: (id: string) => CompetitionEvent | undefined
  getAll: () => CompetitionEvent[]
}

export class InMemoryEventRepository implements EventRepositoryInterface {
  constructor(private events: CompetitionEvent[] = []) {}

  public save = (event: CompetitionEvent) =>
    (this.events = [...this.events, event])
  public getById = (id: string) => this.events.find((e) => e.id === id)
  public getAll = () => this.events
}

export class MongoCompetitionEventRepository
  implements EventRepositoryInterface {
  constructor(private events: CompetitionEvent[] = []) {}

  //here event should go to DB and NOT in the array memory
  public save = (event: CompetitionEvent) =>
    (this.events = [...this.events, event])
  public getById = (id: string) => this.events.find((e) => e.id === id)
  public getAll = () => this.events
}

如果有什么遺漏請告訴我我會編輯帖子

您在這里所做的是連接到本地 MongoDB 現在您必須登錄到 MongoDB 網站,即在這里,登錄后首先要做的是創建一個用戶來訪問數據庫/集合,單擊數據庫訪問,添加用戶,使用身份驗證模式作為密碼來創建用戶,接下來您必須將您的 IP 地址列入白名單,單擊網絡訪問並添加您的 IP 地址或0.0.0.0/0 (這基本上意味着所有/任何IP 地址包括你的可以訪問這個數據庫),之后你必須創建一個新的數據庫,在數據庫中你必須創建 collections,要做到這一點,點擊集群收集並從那里創建你的數據庫,最后完成它連接到數據庫所需的一切,為此單擊ClustersConnect並選擇Connect Application o r Connect using MongoDB Compass選項,這將為您提供連接 URL,如下所示:-


mongodb+srv://<username>:<password>@cluster0-gd3lq.mongodb.net/test


用於MongoDB 羅盤選項


mongodb+srv://<username>:<password>@cluster0-gd3lq.mongodb.net/<dbname>?retryWrites=true&w=majority


像這里的Connect Application選項一樣,您必須使用用於在Database Access中創建用戶的用戶名和密碼更改<username>:<password>並使用您的實際數據庫名稱更改<dbname> 在您有mongoose.connect('mongodb://localhost:27017/CompetitionEvent')的情況下,您必須用連接 URL 替換它。

參考:-

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM