question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'ownerDocument' of null

See original GitHub issue

Intent Upsert a document

Problem I got a “TypeError: Cannot read property ‘ownerDocument’ of null” on function upsertError

Schema Attached (customer.js.txt) customer.js.txt

Code snippet

router.post('/cliente', passwordless.restricted(restrictedOptions), function (req, res) {
	req.check('latitude', 'Não foi possível determinar a latitude do endereço especificado').isDecimal();
	req.check('longitude', 'Não foi possível determinar a longitude do endereço especificado').isDecimal();
	req.check('name', 'O nome especificado não é alfanumérico').notEmpty();
	req.check('email', 'O e-mail especificado não é válido').isEmail();
	req.check('address', 'O endereço especificado não é válido').notEmpty();
	req.getValidationResult()
		.then(function checkPostClienteValidationResult (result) {
			if (result.isEmpty()) {
				const customerData = {
					name: req.sanitize('name').trim(),
					email: req.sanitize('email').normalizeEmail(),
					phone: req.sanitize('phone').trim(),
					address: {
						coordinates: {
							type: 'Point',
							coordinates: [req.sanitize('longitude').toFloat(), req.sanitize('latitude').toFloat()]
						},
						streetAddress: req.sanitize('address').trim(),
						public: ((typeof req.body.address_is_public === 'undefined') ? false : true)
					},
					legalId: req.sanitize('legal_id').trim(),
					notes: req.sanitize('notes').trim()
				};
				const isUpdate = (typeof req.body.id === 'string') && (req.body.id !== '');
				debug('Upserting customer (update: %s), customer data: %j', isUpdate, customerData);
				const updateQuery = {
					_id: (isUpdate ? req.body.id : mongoose.Types.ObjectId())
				};
				const updateOptions = {
					upsert: true,
					runValidators: true,
					setDefaultsOnInsert: true
				};
				Customer.findOneAndUpdate(updateQuery, customerData, updateOptions)
					.exec()
					.then(function upsertDone (customer) {
						debug('Upsert finished, customer data: %j', customer);
						var flashMessage;
						if (isUpdate) {
							flashMessage = 'O cliente foi atualizado com sucesso';
						} else {
							flashMessage = 'O cliente foi adicionado com sucesso';
						}
						req.flash(flashMessage);
						res.redirect(dashboardCustomersPath);
					}, function upsertError (err) {
						debug('Customer upsert error: %s', err);
						for (const key in err.errors) {
							if (err.errors.hasOwnProperty(key)) {
								req.flash('danger', err.errors[key].message);
							}
						}
						res.redirect(dashboardCustomersPath);
					});
			} else {
				res.redirect(dashboardCustomersPath);
			}
		});
});

Notes Mongoose version: 4.7.5 Nodejs version: 7.3.0

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

33reactions
vkarpov15commented, Jan 4, 2017

Add the context option to your query:

const updateOptions = {
  upsert: true,
  runValidators: true,
  setDefaultsOnInsert: true,
  context: 'query'
};

As specified in the mongoose-unique-validator docs

0reactions
vkarpov15commented, Feb 17, 2017

No idea, we don’t have formally supported typescript bindings, so report an issue to whoever maintains yours.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: Cannot read property 'ownerDocument ...
I think my view is returning proper JSON but now data is not being added to the .mainContent div. It gives this error:...
Read more >
Can not read property ownerDocument of null #542 - GitHub
I am getting TypeError: Cannot read property 'ownerDocument' of null even if all the required props are passed to Container and element.
Read more >
Cannot read property 'ownerDocument' of undefined - Laracasts
I put a child component called <page-buttons> in my main Vue component. This component has some buttons floating on the page.
Read more >
cannot read properties of null (reading 'ownerdocument')
I am getting TypeError: Cannot read property 'ownerDocument' of null even if all the required props are passed to Container and element. Line...
Read more >
Cannot read property ownerDocument of null - Mendix Forum
The page doesn't receive an object. Perhaps because of access rights or because there is no object passed to the page.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found