question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems with $push

See original GitHub issue

So, I’m trying to push a mongo ObjectId into an array. The model is the following:

const videos = new Schema({
    name: { type: String, required: true },
    likes: [{ type: ObjectId, ref: 'users', default: [] }],
    dislikes: [{ type: ObjectId, ref: 'users', default: [] }],
    views: { type: Number, default: 0 },
  }, {
    timestamps: true
  });

Then hooks are the following:

before: {
    patch: [
      authenticate('jwt'),
      isValidAction(),
      doLikeAction()
    ]
  }

the doLikeAction is the function not working

const doLikeAction = function(){
  return function(hook) {
    return new Promise((resolve, reject) => {
      hook.app.service('users').find({ query: { _id: hook.params.user._id }})
        .then(res => {
          hook.app.service('videos').update(hook.id, { $push: { likes: res.data[0]._id } })
          .then(res => {
            console.log(res)
            resolve(res)
          })
          .catch(err => {
            console.log(err)
            reject(new errors.BadRequest(constants.BAD_PARAMETERS));
          })
        })
    })
  }
}

It doesn’t say which error it is. The mongo ID are valid. The project is created using the yo generator. Hope you can help! Thank you 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Fraccamancommented, Aug 18, 2017

Everything works fine with update, but it’s not what I meant to use. My bad. If instead of update I use patch (which is what I want to use), then it always fail.

.then(res => {
      return hook.app.service('videos').patch(hook.id, {
        $push: { likes: res.data[0]._id }
      });
    })

The point is that I’m not understanding why it fails and the error is not helping 😦 Moreover, I also would like to populate the likes field of the model. By adding:

const likesSchema = {
  include: {
    service: 'users',
    nameAs: 'like',
    parentField: 'likes',
    childField: '_id',
    query: {
      $select: ['_id', 'email']
    }
  }
};

and

after: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [
      commonHooks.populate({ schema: likesSchema })
    ],
    remove: []
  }

it return a json with a like field in which there are the information relative to the current logged user (which _id is not in the likes array).

Thank for your reply hope you can help! 😃

0reactions
bujji1commented, Apr 17, 2019

@Fraccaman , Sorry my fault , the below code worked for patch

userDetails.patch(contextrecords.receiver_id, {
      $push: { approved_friends: contextrecords.sender_id }
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Common Pushup Mistakes and How to Avoid Them in Your ...
"The most common mistake people make with their pushups is flaring their elbows out as a result of using a hand position that's...
Read more >
Your Push Up Problems Solved | Complete Beginner and Up
Discover how to perform the perfect push up for beginners and progress from not being able to perform the exercise to complete mastery...
Read more >
3 Common Push Up Mistakes & How to Correct Them
Mistake #2 – Hips too low: It is vital to keep your hips level with your shoulders throughout the entire movement including the...
Read more >
Here's the huge problem with push-button ignitions | Driving
Here's the huge problem with push-button ignitions ... per cent of 2014 model cars offering a push button ignition as standard or optional....
Read more >
Why Isn't My Car's Push-Button Start Working?
Dead car battery. If your fob is working but your engine won't crank, your car may have a dead battery. To confirm that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found