[英]Update document in NodeJS and mongoose
I a have a nodeJS app with Typescript and mongoose and I am trying to update a competitionEvent document by adding a subscription to it.我有一个带有 Typescript 和 mongoose 的 nodeJS 应用程序,我正在尝试通过添加订阅来更新 competitionEvent 文档。 Here is my http file:这是我的 http 文件:
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/: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)
})
app.put('/update/:id', async (req: any, res: any) => {
const subs = await CompetitionModel.findOneAndUpdate(
{ id: req.params },
{ subscriptions: req.body },
)
res.send(subs)
})
Here is my mongo file:这是我的蒙戈文件:
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
export const CompetitionSchema = new mongoose.Schema({
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('something went wrong')
})
})
I am not sure if I should make a schema for my subscriptions as well, because the subscriptions belong to the CompetitionSchema.我不确定我是否也应该为我的订阅制作一个架构,因为订阅属于 CompetitionSchema。 Now the error I have when I try to use this /update/:id route is the following:现在,当我尝试使用此 /update/:id 路由时出现的错误如下:
(node:9022) UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{ id: 'whatever' }" at path "id" for model "CompetitionModel" (节点:9022)UnhandledPromiseRejectionWarning:CastError:对于 model“CompetitionModel”的路径“id”处的值“{id:'whatever'}”转换为字符串失败
I am not sure which way to go to have this route working, any ideas?我不确定到 go 的哪种方式可以使这条路线正常工作,有什么想法吗?
In you app.put()
route, you have made a mistake in the code, you are passing the entire req.params
object when you only need to pass the required parameter id
:在你app.put()
路由中,你在代码中犯了一个错误,当你只需要传递必需的参数id
时,你传递了整个req.params
object :
const subs = await CompetitionModel.findOneAndUpdate(
{ id: req.params }, // <------ You are searching for req.params
{ subscriptions: req.body },
)
Fix that with this instead:用这个来解决这个问题:
const subs = await CompetitionModel.findOneAndUpdate(
{ id: req.params.id }, // <------ req.params.id is what you should pass.
{ subscriptions: req.body },
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.