如何发布嵌套数组json对象的关系

我试图发布带有嵌套数组json对象关系待办事项的服务json。当我这样做时,应用会显示下一个错误:

UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ find”

这是我的模型课:

const serviceSchema = new Schema({
  name: String,subject: String,pricePerHour: Number,relatedTodos: 
    [{type: mongoose.Schema.Types.ObjectId,ref:'todos'}],createdAt: Date,updatedAt: Date
});

这是我的发布路线:

app.post('/api/services',async (req,res) => {

  const { name,subject,pricePerHour} = req.body;

  let todos = await Service.findById(req.params.id).TODO.find({});

  if (!todos) {
   return res.status(404).json({
     message: "todos couldn't be found"
    });
  }

  const service = new Service({
   name,description,pricePerHour,relatedTodos

  })


  try {
   let newService = await service.save(); 

   res.status(201).send(newService); 
  } catch (err) {
   if (err.name === 'MongoError') {
  res.status(409).send(err.message);
   }

    res.status(500).send(err);
  }
});

我怎么做错了?

GUDUNVHAI 回答:如何发布嵌套数组json对象的关系

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3132025.html

大家都在问