Javascript 节点和mongodb中的HTTP put请求
我正在mongoDB和node中尝试向competitionEvent对象添加订阅。 我的问题是我只能写一个订阅,不能一个接一个地添加它们。 这是我的http文件:Javascript 节点和mongodb中的HTTP put请求,javascript,node.js,mongodb,typescript,express,Javascript,Node.js,Mongodb,Typescript,Express,我正在mongoDB和node中尝试向competitionEvent对象添加订阅。 我的问题是我只能写一个订阅,不能一个接一个地添加它们。 这是我的http文件: const express = require('express') import * as bodyParser from 'body-parser' // import { eventApplication } from './compositionRoot' import { CompetitionModel } from
const express = require('express')
import * as bodyParser from 'body-parser'
// import { eventApplication } from './compositionRoot'
import { CompetitionModel } from './mongo'
export const app = express()
app.use(bodyParser.json())
// WORKS - find all events
app.get('/events', async (_req: any, res: any) => {
const comp = await CompetitionModel.find()
res.send(comp)
})
// WOKRS - find just one event
app.get('/events/:event_id', async (req: any, res: any) => {
const searchedComp = await CompetitionModel.find(req.params)
res.send(searchedComp)
})
// WORKS - posts a new comp event
app.post('/new-comp', async (req: any, res: any) => {
const data = await new CompetitionModel(req.body).save()
res.json(data)
})
// WORKS - posts a new subscription into a comp
app.put('/update/:event_id', async (req: any, res: any) => {
const subs = await CompetitionModel.findOneAndUpdate(
{ event_id: req.params.event_id },
{ subscriptions: req.body },
)
res.send(subs)
})
// TO TEST - deletes a competition event
app.delete('/delete/:event_id', async (req: any, res: any) => {
const toDel = await CompetitionModel.deleteOne({
event_id: req.params.event_id,
})
res.json(toDel)
})
这是我的mongo文件:
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
export const CompetitionSchema = new mongoose.Schema({
event_id: String,
compName: String,
place: String,
time: String,
subscriptions: [],
date: Date,
cost: {
currency: String,
amount: Number,
},
})
export const CompetitionModel = mongoose.model(
'CompetitionModel',
CompetitionSchema,
)
export const connection = () =>
new Promise((resolve, reject) => {
mongoose.connection.once('open', () => {
resolve()
})
mongoose.connection.once('error', () => {
reject('oooooh shit')
})
})
每次我尝试更改它时,它要么不修改competitionEvent,要么不放置任何内容,要么只是用新订阅替换旧订阅,这毫无意义,我相信您会同意您需要使用
$push
-将新订阅添加到您的竞争中。假设req.body
持有新订阅,您可以执行以下操作:
app.put('/update/:event_id', async (req: any, res: any) => {
const subs = await CompetitionModel.findOneAndUpdate(
{ event_id: req.params.event_id },
{ $push: { subscriptions: req.body }},
)
res.send(subs)
});
首先,修复订阅mongoose的模式。类似下面的模式,以便更好地进行类型转换: 可选
const CompetitionSchema = new mongoose.Schema({
event_id: String,
compName: String,
place: String,
time: String,
subscriptions: [{
//what ever field you wanna add
_id: false //if you don't wanna use it as a sub-document
}],
date: Date,
cost: {
currency: String,
amount: Number,
},
})
然后,在您的CompetitionEvent
控制器中,使用mongo$push
操作符在订阅结束时添加事件订阅,或使用mongo$addToSet
操作符在订阅字段中添加订阅,且不存在任何重复
请记住,$push
不会检查订阅是否唯一,它只会推送javascript Array.push()之类的元素。另一方面,$addToSet
检查订阅是否存在。如果是,则不会添加该订阅。如果否,则将其推送到字段数组
我建议使用$addToSet
,因为它更安全,不会创建同一订阅的任何副本
代码
app.put('/update/:event_id', async (req: any, res: any) => {
const subs = await CompetitionModel.findOneAndUpdate(
{ event_id: req.params.event_id },
{ $addToSet: {subscriptions: req.body}},
)
res.send(subs)
})