配置group.js文件的Model和Router部分以获取并保存多个“ ID”

时间:2019-11-11 21:23:59

标签: node.js rest api mongoose

router.post("/", (req, res, next) => {
//Determine if this Person exists or not 
Identification.findById(req.body.The_ID)//find that ID 
  .then(id => {//commence checking 
    if (!id) {//setting if not criteria case 
      return res.status(404).json({//response that comes back if non-existent ID entered 
        message: "Person not found"//message to user on screen
      });
    }
    const group = new Group({//Creating new instance of Group objects established in model file
      _id: mongoose.Types.ObjectId(),////generate origina ID code for this Group
      quantity: req.body.quantity,//amount of IDs(people in this group)
      product: req.body.The_ID//target ID(person) to come into group
    });
    return group.save();//save this group to data base 
  })
  .then(result => {
    console.log(result);
    res.status(201).json({
      message: "Order stored",
      createdGroup: {
        _id: result._id,
       person: result.person,
        quantity: result.quantity
      },
      request: {
        type: "GET",
        url: "http://localhost:3000/orders/" + result._id
      }
    });
  })
  .catch(err => {
    console.log(err);
    res.status(500).json({
      error: err
    });
  });

});

const mongoose = require('mongoose');

const groupSchema = mongoose.Schema({     _id:mongoose.Schema.Types.ObjectId,     人:{type:mongoose.Schema.Types.ObjectId,编号:'Identification',要求:true},     数量:{类型:编号,默认值:1} });

module.exports = mongoose.model('Group',groupSchema);

我真正想要做的就是将其配置为采用单个或多个ID(人)进行保存。

0 个答案:

没有答案