使用来自mongoose

时间:2016-12-07 16:10:44

标签: node.js mongodb express mongoose handlebars.js

您好,

我正在尝试从mongoose获取价值并使用它填充文本框。因此,基本上当我单击“编辑”按钮时,我希望部门的名称显示在“部门名称”文本框中。我试过多次但失败了。 (例如$('#departmentName').val(department.name))我该如何正确地做到这一点?谢谢。

我的部门车把

<h2>Departments List</h2>
<div class="row">
	<div class="col-lg-12">
		<table class="table">
			<thead>
				<tr>
					<th>Name</th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<form method="POST" action="/departments/create_department">
						<td><input class="form-control" type="text" placeholder="Department Name" name="departmentName" id="departmentName"></td>
						<td><button class="btn btn-primary" type="sumit">Add Department</button></td>
					</form>
					<td><button class="btn btn-info">Update</button></td>
					<td><button class="btn btn-info">Clear</button></td>
				</tr>
				{{#each departments}}
				<tr>
					<form method="POST" action="/departments/remove_department/{{_id}}?_method=DELETE">
						<td>{{name}}</td>
						<td><button class="btn btn-danger" type="submit">Remove</button></td>
					</form>
					<form method="GET" action="/departments/edit_department/{{_id}}?">
						<td><button class="btn btn-warning" value={{id}} type="submit">Edit</button></td>
					</form>
				</tr>
				{{/each}}
			</tbody>
		</table>	
	</div>
</div>

我的部门控制员

var express = require('express');
var router = express.Router();
var passport = require('passport');
var mongoose = require('mongoose');

var Departments = require('../models/department');
var User = require('../models/user');

// List of departments
router.get('/add_department', userAuthenticated, function(req, res, next) {
	Departments.find(function(err, departments) {
		if (err) {
			throw err;
		}
		res.render('add_department', {title:'Add New Department', departments: departments, name: departments.name, id: departments._id})
	})
});

// Edit department
router.get('/edit_department/:id', userAuthenticated, function(req, res, next) {
	var departmentName = "";
	Departments.findById(req.params.id).exec(function(err, department) {
		if (err) {
			throw err;
		} else {
			$('#departmentName').val(department.name);
			res.redirect('/departments/add_department');
		}
	})
});

1 个答案:

答案 0 :(得分:0)

您可以尝试更改手柄中的输入字段以显示您从猫鼬查询的值

更改此

<td><input class="form-control" type="text" placeholder="Department Name" name="departmentName" id="departmentName"></td>

到这个

<td><input class="form-control" type="text" value={{name}} name="departmentName" id="departmentName"></td>

然后从路线中取出

$('#departmentName').val(department.name);

更新:

这样的事情

// List of departments
router.get('/add_department', userAuthenticated, function(req, res, next) {

// grab the deparment from the req
var departmentFromEdit ="";
if(req.query.dn){
    departmentFromEdit = req.query.dn;
}
Departments.find(function(err, departments) {
    if (err) {
        throw err;
    }
    res.render('add_department', {title:'Add New Department', departments: departments, name: departments.name, id: departments._id})
})});

// Edit department
router.get('/edit_department/:id', userAuthenticated, function(req, res, next) {
var departmentName = "";
Departments.findById(req.params.id).exec(function(err, department) {
    if (err) {
        throw err;
    } else {
        departmentName = encodeURI(department.name);
        // dn for departmentName or defaultName
        res.redirect('/departments/add_department?dn=' + departmentName);
    }
})});