简体   繁体   中英

add/remove an element into an id array of an object with mongoose

Hello, I have two models user and recipe :

const userSchema = new mongoose.Schema({
  _id: {
    type: String,
    required: true
  },
  login: {
    type: String,
    match: /^[a-zA-Z0-9-_]+$/
  },
  password: {
    type: String
  }
});


const recipeSchema = new mongoose.Schema({
  recipeID: {
    type: Number
  },
  title: {
    type: String
  },
  comments: [
    {
      text: { type: String, required: true },
      author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
      postedAt: { type: Date, default: Date.now, index: true },
    },
  ],
  ingredients: [
    {
      ingredient: { type: String, required: true },
      quantity: { type: String },
      unit: {
        type: String,
        default: ''
      },
      index: { type: Number, required: true }
    }
  ],
  steps: [{
    text: { type: String, required: true },
    index: { type: Number, required: true },
  }],
  tags: { type: [String], index: true },
  validatedBy: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]
});

client side :

   function transformUsersToIDs(arr) {
      const userIDs = [];
      arr.forEach((user) => {
         userIDs.push(user._id);
      });
      return userIDs;
    }

...

    const curRecipe = this.state.currentRecipe;
    const valArr = transformUsersToIDs(curRecipe.validatedBy);
    const i = valArr.indexOf(this.state.user._id);
    if (i > -1) {
      curRecipe.validatedBy = valArr.slice(0, i).concat(valArr.slice(i + 1));
    } else {
      curRecipe.validatedBy = valArr.push(this.state.user._id);
    }
    curRecipe.comments.forEach((com) => {
      com.author = com.author._id;
    });

    axios.put(`/api/recipes/${curRecipe.recipeID}`, curRecipe)
      .then((res) => {
        this.setState({ currentRecipe: res.data });
      });

and server side:

// update a recipe
  app.put('/api/recipes/:id', (req, res, next) => {
    Recipe.findOne({ recipeID: Number(req.params.id) })
      .exec()
      .then((recipe) => {
        recipe.category = req.body.category;
        recipe.title = req.body.title;
        recipe.comments = req.body.comments;
        recipe.ingredients = req.body.ingredients;
        recipe.steps = req.body.steps;
        recipe.tags = req.body.tags;
        recipe.validatedBy = req.body.validatedBy;

        recipe.save()
          .then(() => res.json(recipe))
          .catch(err => next(err));
      })
      .catch(err => next(err));
  });

and I try to update a recipe with add or remove an element in the validatedBy array... The problem is that my recipe is populated, I transform the comments.author into a string (representing a user id) and the validatedBy into a string array (representing the user id). When I try to update I have this error :

ValidationError: Recipe validation failed: comments.0.author: Cast to ObjectId failed for value "@funnybobby" at path "author", validatedBy: Cast to [ObjectId] failed for value "["@funnybobby"]" at path "validatedBy"

Would anyone have an idea where my problem is coming from?

it's not works ... I think I'm making a mistake but I do not see which one

my code :

Recipe.update({ recipeID: req.params.id }, { $push: { validatedBy: new mongoose.Types.ObjectId(req.body.user) } }, (err) => {
  if (err) {
    next(err);
  } else {
    Recipe.find({ recipeID: Number(req.params.id) })
      .exec()
      .then((recipe) => res.json(recipe))
      .catch(err => next(err));
  }
});

});

the error : Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

Finally, I found a solution :

 app.put('/api/recipes/validatedBy/add/:id', (req, res, next) => {
    User.findById(req.body.user)
      .then((u) => {
        Recipe.findOne({ recipeID: Number(req.params.id) })
          .populate('validatedBy')
          .then((recipe) => {
            recipe.validatedBy.push(u);
            recipe.save((err) => {
              if (err) {
                res.send(err);
              }
              res.json(recipe);
            });
          });
      })
      .catch(err => next(err));
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM