Skip to content
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

Closed
iamdeit opened this issue Sep 11, 2018 · 3 comments
Closed

Bug: Hook function cannot access request-context variables #7001

iamdeit opened this issue Sep 11, 2018 · 3 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@iamdeit
Copy link

iamdeit commented Sep 11, 2018

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:

const httpContext = require('express-http-context');

schema.pre('save', function () {
  const email = httpContext.get('email');
  console.log(email); // Prints the correct value!
  this.updatedBy = email; 
}


schema.pre('findOneAndUpdate', function () {
  const email = httpContext.get('email');
  console.log(email); // Prints undefined 
}

I also tried with post hooks but didn't work.

I'm using Mongo v3 and mongoose v5.1.5

@vkarpov15
Copy link
Collaborator

Thanks for reporting, we will look into this asap

@vkarpov15 vkarpov15 modified the milestones: 5.2.16, 5.2.18 Sep 16, 2018
@vkarpov15 vkarpov15 modified the milestones: 5.2.18, 5.2.20 Sep 27, 2018
@vkarpov15
Copy link
Collaborator

The below script seems to work fine for me in Mongoose 5.1.5 and 5.3.1, with [email protected], [email protected]. Can you try modifying the below script to demonstrate your issue?

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 vkarpov15 removed this from the 5.3.2 milestone Oct 5, 2018
@vkarpov15 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
@mtbnunu
Copy link

mtbnunu commented Nov 29, 2018

I was able to modify your script to reproduce the error, It showed up as I changed callback approach to async/await

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("find", function(next) {
		console.log(httpContext.get("test"));
		next();
	});

	const User = mongoose.model("User", userSchema);

	app.get("/", async (req, res) => {
		httpContext.set("test", "42");
		const user = await User.find({});
		res.json({ ok: 1 });
	});

	await app.listen(3003);

	await superagent.get("http://localhost:3003/");

	console.log("done");
	process.exit(0);
}

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.
Projects
None yet
Development

No branches or pull requests

3 participants