Lyrical-GraphQL icon indicating copy to clipboard operation
Lyrical-GraphQL copied to clipboard

The add lyrics query returns an error but saves in the database

Open Jay-Topher opened this issue 6 years ago • 9 comments

error

I believe there's an issue either with the db model or the addLyrics mutation

Jay-Topher avatar Jan 21 '20 08:01 Jay-Topher

yes. I'm also having the same issue.

vishalisakar avatar Jan 22 '20 08:01 vishalisakar

Anyone with answers should please notify us here, so we can move forward with the course, this is a huge blocker

Jay-Topher avatar Jan 22 '20 16:01 Jay-Topher

How far along are you in the video?

levelingup avatar Feb 08 '20 16:02 levelingup

I'm currently in Client side graphql >> working through the schema

Jay-Topher avatar Feb 17 '20 08:02 Jay-Topher

That should be the fourth video of the 6th section

Jay-Topher avatar Feb 17 '20 08:02 Jay-Topher

So you cloned the repo, are you using monogodb atlas? https://www.mongodb.com/cloud/atlas

levelingup avatar Feb 17 '20 14:02 levelingup

Finally, I got the solution. Thank you @PostIt59, by your solution only I could solve it.

I hope u can try this or you can check his solution in issue page at #17

In server/models/song.js just add:

usePushEach : true

onst SongSchema = new Schema({
  title: { type: String },
  user: {
    type: Schema.Types.ObjectId,
    ref: 'user'
  },
  lyrics: [{
    type: Schema.Types.ObjectId,
    ref: 'lyric'
  }]
}, {
  usePushEach : true
});

then change the addLyric as follows

song.lyrics.push(lyric) to song.lyrics.push(lyric.id)

SongSchema.statics.addLyric = function(id, content) {
  const Lyric = mongoose.model('lyric');

  return this.findById(id)
    .then(song => {
      const lyric = new Lyric({ content, song })
      song.lyrics.push(lyric.id)
      return Promise.all([lyric.save(), song.save()])
        .then(([lyric, song]) => song);
    });
}


vishalisakar avatar Feb 18 '20 04:02 vishalisakar

Yes @levelingup , I'm using mongoDB Atlas, thanks @vishalisakar for the update, I'll try it now

Jay-Topher avatar Feb 18 '20 11:02 Jay-Topher

Thanks guys, it worked. Now I can move on with the course, I think I should leave this here for people who would run into this problem so they can find the solution here

Jay-Topher avatar Feb 19 '20 06:02 Jay-Topher