如何在我的mongo模式中增加一个喜欢的计数器

时间:2019-06-08 13:18:43

标签: reactjs mongodb express redux

我想为我的Mongo模式添加一个like计数器。我在Redux中对其进行了更新,但刷新后丢失了,无法将其保存到数据库中。我还附上了删除流程(它可以正常工作),只是为了提供一些上下文。

这是我的动作创建者

export const remove = id => ({
  type: REMOVE_MESSAGE,
  id
});

export const like = (id, messages) => {
  return {
    type: LIKE_MESSAGE,
    id,
    messages
  };
};

export const likeMessage = (user_id, message_id) => {
  return dispatch => {
    return apiCall("put", `/api/users/${user_id}/messages/${message_id}`)
      .then(() => dispatch(like(message_id)))
      .catch(err => addError(err.message));
  };
};

export const removeMessage = (user_id, message_id) => {
  return dispatch => {
    return apiCall("delete", `/api/users/${user_id}/messages/${message_id}`)
      .then(() => dispatch(remove(message_id)))
      .catch(err => addError(err.message));
  };
};

和我的减速器

const message = (state = [], action) => {
  switch (action.type) {
    case LOAD_MESSAGES:
      return [...action.messages];

    case REMOVE_MESSAGE:
      return state.filter(message => message._id !== action.id);

    case LIKE_MESSAGE:
      return state.map(message => {
        if (message._id === action.id) {
          return Object.assign({}, message, { likes: message.likes + 1 });
        }
        return message;
      });

    default:
      return state;
  }
};

直到这里,它似乎仍然有效。这些是我的服务器端路由处理程序,再次是likeMessage路由给我带来了问题,我很确定这是问题所在。


exports.deleteMessage = async function(req, res, next) {
  try {
    let foundMessage = await db.Message.findById(req.params.message_id);
    await foundMessage.remove();
    return res.status(200).json(foundMessage);
  } catch (err) {
    return next(err);
  }
};

exports.likeMessage = async function(req, res, next) {
  try {
    let like = await db.Message.findByIdAndUpdate(
      req.params.message_id,
      {
        $inc: { "message.$.likes": 1 }
      },
      { new: true }
    );
    await like.save();
    return res.status(200).json(like);
  } catch (err) {
    return next(err);
  }
};

这是我的消息模型

const mongoose = require("mongoose");
const User = require("./user");

const messageSchema = new mongoose.Schema(
  {
    text: {
      type: String,
      required: true,
      maxLength: 160
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    },
    likes: {
      type: Number,
      default: 0
    }
  },
  {
    timestamps: true
  }
);

messageSchema.pre("remove", async function(next) {
  try {
    let user = await User.findById(this.user);
    user.messages.remove(this.id);
    await user.save();
    return next();
  } catch (err) {
    return next(err);
  }
});

const Message = mongoose.model("Message", messageSchema);
module.exports = Message;

和我的路线文件,以防万一。

const express = require("express");
const router = express.Router({ mergeParams: true });

const {
  createMessage,
  getMessage,
  deleteMessage,
  likeMessage
} = require("../handlers/messages");

router.route("/").post(createMessage);

router
  .route("/:message_id")
  .get(getMessage)
  .delete(deleteMessage)
  .put(likeMessage);

module.exports = router;

为所有代码表示歉意,我敢肯定这与我在路由处理程序中处理$ inc运算符的方式有关。任何建议或技巧将不胜感激。

0 个答案:

没有答案