不能在我的突变中使用快速会话

问题描述 投票:0回答:1

大家好,我在快速会议上遇到问题。我已经创建了带有express-session的会话,并在我的apollo-server-express中使用了它,但是当我想使用该会话存储用户ID并再次在查询中使用该会话时,在我的登录突变内部,它将更改为undefined 。有什么问题?

这是我使用mongo-connect创建会话的index.js:

const MongoStore = mongoconnect(session);    
const SERVER = new ApolloServer({
    typeDefs,
    resolvers,
    cors: {
        origin: '*',
        credentials: true
    },
    playground: {
        endpoint: `http://localhost:3600/graphql`,
        settings: {
            'editor.theme': 'dark'
        }
    },
    context: ({ req }) => {
        return {
            req,
            session: req.session
        }
    }
});

app.use(
    session({
        store: new MongoStore({
            mongooseConnection: mongoose.connection
        }),
        secret: "mysecret-ssss",
        resave: false,
        saveUninitialized: false,
        cookie: {
            maxAge: 1000 * 60 * 60 * 2,
            sameSite: true,
            secure: true
        }
    })
);

这是我的查询:

Query: {
   circularLetters: async (parent, args, context, info) => {
            const options = {
                page: args.page,
                limit: args.limit
            };
            const circularLetter = await CircularLetters.paginate({}, options, (err, result) => {
                return result.docs;
            });
            console.log(context.session.userId)

            return circularLetter;
        },
}

还有我想在会话中存储我的用户ID的突变:

Mutation: {
     login: async (parent, args, context, info) => {
            const user = await Users.findOne({
                personelNumber: args.data.personelNumber
            }, function (err, myUser) {
                console.log(err);
            });

            if (!user) {
                throw new Error("User not found");
            }

            const isMatch = await bcrypt.compare(args.data.password, user.password);

            if (!isMatch) {
                throw new Error("Wrong password");
            }

            context.session.userId = user.id;
            console.log(context.session);

            return {
                user,
                token: generateToken(user.id)
            }
        },
}
mongodb mongoose graphql express-session connect-mongo
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.