javascript - Node.JS/ express /蒙戈 : TypeError: Cannot read property 'location' of undefined

标签 javascript node.js mongodb

我一直在寻找这个问题的其他解决方案,但我在其他地方看到的解决方案似乎都不适合我。我有一个要提交给数据库的表单。我正在使用 node.JS、express、mongo 和 pug 来呈现 HTML。

这是我的表格:

.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 ×

这是我的服务器端 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'); 
})

最佳答案

术语 locationwindow 的保留属性(property)浏览器中的对象。当您定义一个 id元素上的属性,浏览器会将 DOM 元素分配给 window 中的属性,值为 id .

例如,对于元素

<input id="location" />

您创建可通过以下方式访问的属性

window.location

然而,window.location是浏览器的只读属性。

尝试重命名 <input>元素

关于javascript - Node.JS/ express /蒙戈 : TypeError: Cannot read property 'location' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52157443/

相关文章:

javascript - 处理ajax调用后的php错误

node.js - 使用sails-hook-sequelize进行Sails单元测试

javascript - Node.js 共享内存和原子文档以及操作方法

linux - 通过 Node 0.10.25 安装 jshint 时出现详细的错误 json 错误

javascript - 带有 ES6 模板字符串的 MongoDB 对象键

javascript - SVG 路径在 D3 力网络图中无法正确渲染

javascript - 使用Jquery ajax调用Controller中的ActionResult方法并返回数据

mongodb - 如何在 mongodb 的 $or 语句中合并两个 $and 语句?

javascript - Mongodb findOne 但首先检查请求是否为空

javascript - 如何正确地链接 Vuex 操作,并正确地改变调用之间的状态?