[英]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,要做到这一点,点击集群,收集并从那里创建你的数据库,最后完成它连接到数据库所需的一切,为此单击Clusters , Connect并选择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.