-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Hook function cannot access request-context variables #7001
Labels
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Comments
Thanks for reporting, we will look into this asap |
The below script seems to work fine for me in Mongoose 5.1.5 and 5.3.1, with const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
const httpContext = require('express-http-context');
const superagent = require('superagent');
const express = require('express');
const GITHUB_ISSUE = `gh7001`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect(connectionString);
await mongoose.connection.dropDatabase();
const app = express();
app.use(httpContext.middleware);
const userSchema = new Schema({ name: String });
userSchema.pre('findOneAndUpdate', function(next) {
console.log(httpContext.get('test'));
next();
});
const User = mongoose.model('User', userSchema);
app.get('/', function(req, res) {
httpContext.set('test', '42');
User.findOneAndUpdate({}, { name: 'Test' }, function() {
res.json({ ok: 1 });
});
});
await app.listen(3003);
await superagent.get('http://localhost:3003/');
console.log('done');
process.exit(0);
} |
vkarpov15
added
the
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
label
Oct 5, 2018
I was able to modify your script to reproduce the error, It showed up as I changed callback approach to async/await
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
I have been working on adding an extra field (updatedBy) to every model by using a pre-findOneAndUpdate hook. The value I'm trying to assign is coming from a request-context object, where you can save global variables per request. The problem is that if I try to access the global variables via request-context object, it says undefined, and this only happens inside the pre hook for findOneAndUpdate.
I have tried to use https://www.npmjs.com/package/express-http-context and also https://www.npmjs.com/package/request-context but none of them seem to work. The weirdes part is that they work in the pre save hook.
For example:
I also tried with post hooks but didn't work.
I'm using Mongo v3 and mongoose v5.1.5
The text was updated successfully, but these errors were encountered: