我正在尝试在 mongodb 中进行简单的更新操作。我已经使用 node.js、Express.js 和 Jade.js 成功完成了它。现在我想在我现有的应用程序中使用 restify.js 来获取 rest api。但它抛出“Express 500 错误:传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串”
这是我的 index.jade(表单部分):
form( method="get", action="/employee/:id/edit")
input(name="_id", type="hidden", value=employee._id.toHexString())
input(name="com", type="hidden", value=''+employee.comment)
input(name="titl", type="hidden", value=''+employee.title)
input(name="nam", type="hidden", value=''+employee.name)
input(id="edit", value="Edit", type="submit", class="btn btn-primary")
这是我的employee_edit.jade:
form( method="post")
span.label My comment type is :
select(name='title')
- for (var i=0; i<2; i++)
if titl==arr2[i]
option(value=arr2[i], selected='selected')
=''+arr2[i]
else
option(value=arr2[i])
=''+arr2[i]
span.label My comment is for :
select(name='name', selectedInex=3)
- for (var i=0; i<22; i++)
if nam==arr[i]
option(value=arr[i], selected='selected')
=''+arr[i]
else
option(value=arr[i])
=''+arr[i]
span.label Comment :
textarea(rows='4', cols='50', name='comment', id='com')
=''+com
input(type="submit", value="Update")
这是我的 app.js 文件,它是我的应用程序的起点:
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, EmployeeProvider = require('./employeeprovider').EmployeeProvider
, myDate=require('date-utils');
var restify = require('restify');
var restServer = restify.createServer({ name: 'my-api' });
var app = express();
app.configure(function(){
app.set('port', 3333);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
var employeeProvider= new EmployeeProvider('localhost', 27017);
//Routes of express jade
//update an employee Feedback
app.get('/employee/:id/edit', function(req, res) {
employeeProvider.findById(req.param('_id'), function(error, employee) {
res.render('employee_edit',
{
//employee: employee,
empId: req.param('_id'),
com: req.param('com'),
titl: req.param('titl'),
nam: req.param('nam')
});
});
});
//save updated employee Feedback
app.post('/employee/:id/edit', function(req, res) {
employeeProvider.update(req.param('_id').toString(),{
title: req.param('title'),
name: req.param('name'),
comment: req.param('comment'),
created_at: new Date()
}, function(error, docs) {
res.redirect('/')
});
});
app.listen(3333);
我为mongodb连接编写了employeeprovider.js:
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
EmployeeProvider = function(host, port) {
this.db= new Db('node-mongo-employee', new Server(host, port, {safe: false}
{auto_reconnect: true}, {}));
this.db.open(function(){});
};
EmployeeProvider.prototype.getCollection= function(callback) {
this.db.collection('employees', function(error, employee_collection) {
if( error ) callback(error);
else callback(null, employee_collection);
});
};
EmployeeProvider.prototype.update = function(employeeId, employees, callback) {
this.getCollection(function(error, employee_collection) {
console.log('now in update'+employeeId);
if( error ) callback(error);
else {
employee_collection.update(
{_id: employee_collection.db.bson_serializer.ObjectID.createFromHexString(employeeId)},
employees,
function(error, employees) {
if(error) callback(error);
else callback(null, employees)
});
}
});
};
exports.EmployeeProvider = EmployeeProvider;
现在通过单击 index.jade 上的编辑按钮,它应该打开 employee_edit.jade 来编辑员工。但它显示错误。地址栏中的地址是:
http://localhost:3333/employee/:id/edit?_id=5215ad336ad5d6780d000001&com=dfsa&titl=Suggestion&nam=Abhishek
如果我删除 app.js 的第 8 行和第 9 行,它会起作用
var restify = require('restify');
var restServer = restify.createServer({ name: 'my-api' });
但我想在我的应用程序中使用restify,我该怎么办???谢谢..!