简体   繁体   中英

Node.JS/Express/Mongo: TypeError: Cannot read property 'location' of undefined

I've gone searching for other solutions to this problem and none of the solutions I've seen elsewhere seem to work for me. I have a form that I want to submit to a database. I'm using node.JS, express, mongo, and pug to render the HTML.

Here's my form:

.modal-content
form(method='POST' action='/insert' id='newReminders')
  input(type='text' name='location' id='location' placeholder='location')
  br
  input(type='textarea' name='reminder' rows='4' id='reminder' placeholder='type your reminder here')
  br
  button(type='submit' id='saveButton') save
span.close-button ×

And here's my server-side JS:

 app.post('/insert', (req, res) => { var dokoEntry = { reminder: req.body.location, content: req.body.reminder }; mongo.connect(url, function (err, db) { assert.equal(null, err); db.collection('reminders').insertOne(item, (err, result) => { assert.equal(null, err); console.log('Reminder inserted'); db.close(); }) }) res.redirect('/checkLocation'); }) 

The term location is a reserved property of The window object in browsers. When you define an id attribute on an element, the browser will assign the DOM element to a property in window by the value of id .

So, for example, for the element

<input id="location" />

your creating a property accessible via

window.location

However, window.location is a read-only property of the browser.

Try renaming that <input> element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM