Невозможно отправить в массив с помощью mongoose findOneAndUpdate () - PullRequest
0 голосов
/ 17 сентября 2018

Моя dbUsers коллекция выглядит следующим образом:

const userSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  first_name: {
    type: String,
    required: true,
  },
  last_name: {
    type: String,
    required: true,
  },
  posts: [
    {
      id: {
        type: String,
        required: true,
      },
      title: {
        type: String,
        required: true,
      },
      content: {
        type: String,
        required: true,
      },
    }
  ],
});

module.exports = mongoose.model('dbUser', userSchema);

Я пытаюсь вставить новую запись в один из массивов posts [] документов, используямутация GraphQL:

Mutation: {
    addPost: async (parent, args) => {
      let output = {};
      let output2 = {};
      const newpost = new dbPost({
        _id: new mongoose.Types.ObjectId(),
        title: args.title,
        content: args.content,
        author: {
          id: args.authorid,
          first_name: args.authorfirstname,
          last_name: args.authorlastname,
        }
      });
      const newpost2 = {
        id: args.id,
        title: args.title,
        content: args.content,
      };
      output = await newpost.save();
      output2 = await dbUser.findOneAndUpdate(
        { _id: args.authorid },
        { $push: { posts: newpost2 } },
        true
      );
      console.log(output2);
      return output;
    },
  },

Как видите, я пытаюсь обновить две разные коллекции в этой мутации, dbPosts и dbUsers .Обновление до dbPosts работает нормально, но, похоже, что на dbUsers выходит из строя.Я вижу ошибку:

«Невозможно использовать оператор« in »для поиска« useFindAndModify »в true»

Что-то не так с моим синтаксисом findOneAndUpdate()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...