猫鼬的findByIdAndUpdate不会一致地增加我的MongoDB文档的计数字段

时间:2020-03-18 21:18:28

标签: javascript node.js mongodb mongoose

我正在尝试制作一个每周计划应用程序,以便使用Mongoose,MongoDB和GraphQL服药。我有一个editPillCount突变,其中传递了一个医学id GraphQLNonNull(GraphQLID)值和一个update GraphQLNonNull(GraphQLInt)值。

使用以下GraphQL突变:

mutation {
  editPillCount(id:"5e7278d38be630025866a470", update: 1) {
    name
    count
  }
}

给出输出:

{
  "data": {
    "editPillCount": null
  }
}

我的问题:即使我使用的是{new: true},resolve函数也不会返回最新的值,并且count字段也不总是以update的值递增。我该如何解决?

到目前为止,最初添加新药物的其他突变形式以及其他查询均正常运行。

医学模式:medicine.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema; 

const medicineSchema = new Schema({
    id: String,
    count: Number,
    name: String,
    times: [Number],
    dayNames: [String]
});
module.exports = mongoose.model('Medicine', medicineSchema);

星期几模式:dayOfWeek.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema; 

const dayOfWeekSchema = new Schema({
    _id: String
});

module.exports = mongoose.model('DayOfWeek', dayOfWeekSchema); 

根本查询:schema.js

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        medicine: {
            type: MedicineType,
            args: { id: { type: GraphQLID }},
            resolve(parent, args) {
                return Medicine.findById(args.id);
            }
        },

        dayOfWeek: {
            type: DayOfWeekType,
            args: { name: { type: GraphQLString }},
            resolve(parent, args) {
                return DayOfWeek.findById(args.name); 
            }
        }, 

        medicines: {
            type: new  GraphQLList(MedicineType),
            resolve(parent, args) {
                return Medicine.find({});
            }
        },

        dayOfWeeks: {
            type: new GraphQLList(DayOfWeekType),
            resolve(parent, args) {
                return DayOfWeek.find({}); 
            }
        }
    }
});

Edit1:我正在打印console.log("Medicine updated successfully"),它只打印一次,但添加两次或三遍。 突变:schema.js

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        editPillCount: {
            type: MedicineType,
            args: {
                id: { type: new GraphQLNonNull(GraphQLID) },
                update: { type: new GraphQLNonNull(GraphQLInt) }
            },

            resolve (parent, args) {

                return Medicine.findByIdAndUpdate(
                    args.id,
                    { $inc: { count: args.update }},
                    { new: true, useFindAndModify: false },
                    function(err, response) {
                        if (err) {
                            console.log("Error updating medicine counts.");  
                        } else {
                            console.log("Medicine updated successfully.");
                        }
                    }
                );
            }
        }
    }
});

架构导出schema.js

module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation: Mutation
}); 

0 个答案:

没有答案