mongodb在事件注释中将用户添加到喜欢的数组

时间:2018-07-22 11:31:19

标签: javascript node.js mongodb mongoose

所以我试图将用户添加到likes数组,当前无法,因为我找不到如何获取事件注释,findone失败以及使用findone(在事件上获取捕获错误)功能的方法。注释),尝试了映射,但是由于架构看起来像这样(请参阅下文),因此有点混乱。

事件模型

  creator: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
  comments: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: 'User'
      },
      text: {
        type: String,
        required: true
      },
      likes: [
        {
          user: {
            type: Schema.Types.ObjectId,
            ref: 'User'
          }
        }
      ]
    }
  ]
}

我当前的功能:

  commentLike: async (req, res) => {
    Events.findById(req.params.idas)
      .then((event) => {
        event.comments.findById(req.params.commentID) //this fails maybe since its not schema model anymore?
          .then((comment) => {
            if (comment.likes.filter((like) => { return like.user.toString() === req.params.id; }).length > 0) {
              return res.status(400).json({ alreadyliked: 'already liked' });
            }
            event.comment.likes.unshift({ user: req.params.id });
            return event.save().then((resevent) => { return res.json(resevent); });
          });
      })
      .catch((err) => {
        res.status(404).json({ nopostfound: 'No commnet found' });
      });
  }

我通过了三个参数 id-是用户ID idas-这是事件ID commentID-评论ID

1 个答案:

答案 0 :(得分:1)

您可以尝试

 private void loadLocationForThisUser(String email) {
        Query user_location = locations.orderByChild("email").equalTo(email);

        user_location.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
                {
                    Tracking tracking = postSnapShot.getValue(Tracking.class);

                    //add marker for friend location
                    LatLng friendLocation = new LatLng(Double.parseDouble(tracking.getLat()),
                            Double.parseDouble(tracking.getLng()));

                    //create location from user coordinates
                    Location currentUser = new Location("");
                    currentUser.setLatitude(lat);
                    currentUser.setLongitude(lng);

                    //create location from friend coordinates
                    Location friend = new Location("");
                    friend.setLatitude(Double.parseDouble(tracking.getLat()));
                    friend.setLongitude(Double.parseDouble(tracking.getLng()));

                    //create function calculate distance between location
//                    distance(currentUser,friend);

                    //add friend marker on map
                    mMap.addMarker(new MarkerOptions()
                    .position(friendLocation)
                    .title(tracking.getEmail())
                            .snippet("Distance "+new DecimalFormat("#.#").format( distance(currentUser,friend)))
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lng),12.0f));
                }
                //create marker for current user
                LatLng current = new LatLng(lat,lng);
                mMap.addMarker(new MarkerOptions().position(current).title(FirebaseAuth.getInstance().getCurrentUser().getEmail()));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }