我正在编写一个简单的应用程序,试图使用MVC模式将数据从IONIC表单传递到json文件中。 我正在努力从表单传递这些数据。 我正在使用IONIC,JavaScript,NodeJS / ExpressJs。
这是我的html表单:
<ion-content>
<ion-header translucent>
<ion-toolbar color="primary">
<ion-title>Add a bug</ion-title>
</ion-toolbar>
</ion-header>
<form class="bug-form" action="/add-bug" method="POST">
<ion-item>
<ion-label position="fixed">Bug title</ion-label>
<ion-input id="name"></ion-input>
</ion-item>
<ion-item>
<ion-label position="fixed">Bug description</ion-label>
<ion-input id="desc"></ion-input>
</ion-item>
<ion-item>
<ion-label position="fixed">Date</ion-label>
<ion-input id="date" type="date"></ion-input>
</ion-item>
<ion-item>
<ion-label position="fixed">Assigned to</ion-label>
<ion-input id="assignedTo"></ion-input>
</ion-item>
<ion-item>
<ion-label position="fixed">Assigned by</ion-label>
<ion-input id="assignedBy"></ion-input>
</ion-item>
<ion-button type="submit" color="primary">Submit</ion-button>
<ion-button href="view-bug" color="warning">Cancle</ion-button>
</form>
</ion-content>
routes.js中附带的代码:
router.post('/add-bug', bugController.postAddBug);
最后,我在控制器中的发布请求:
exports.postAddBug = (req, res, next) =>{
let name = document.getElementById('name');
const bugs = new Bug(
req.body.name,
req.body.desc,
req.body.date,
req.body.assignedTo,
req.body.assignedBy
);
console.log(bugs);
bugs.save();
res.redirect('/view-bug');
};
显然,我试图以错误的方式传递数据,因为所有字段均未定义。