[英]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.
我正在使用node.JS,express,mongo和pug来呈现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: 这是我的服务器端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. 术语
location
是浏览器中window
对象的保留属性。 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
. 当您在元素上定义
id
属性时,浏览器将通过id
的值将DOM元素分配给window中的属性。
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. 但是,
window.location
是浏览器的只读属性。
Try renaming that <input>
element 尝试重命名该
<input>
元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.