2017-08-08 232 views
0

您好我是nodejs的新手,我尝试重定向到一个新页面以显示单个记录,当我点击编辑按钮时。我不确定路线如何工作。 如果我浏览到: http://localhost:1337/我看到一个包含所有用户的表格。我添加了一个说编辑的列,但是当我点击编辑时,我试图运行一个查找记录然后打开帕格页的函数:localhost:1337/userInfo。如何在nodejs中打开一个新页面并显示用户数据

如果我只是浏览到:localhost:1337/userInfo,然后我看到与http://localhost:1337/相同的表,因为它链接到具有所有代码的global.js文件。所以我不确定这个路由是如何工作的,以及如何去编辑userInfo页面,并且只查看global.js中函数的单个记录。下面的一些代码:

users.js

router.get('/findOneUser/:id', function (req, res) { 

var db = req.db; 
var id = req.params.id; 
var reg = new RegExp(id) 
var collection = db.get('userlist'); 
ccollection.find({ '_id': { $regex: reg }}, function (e, docs) { 
    res.json(docs); 
}); 

});

global.js

function populateTable(newuser) { 

// Empty content string 
var tableContent = ''; 
Json = '/users/userlist'; 

//Compile getJSON Statement 
if (newuser !== '') { 


    Json = '/users/findUser/' + newuser; 

} 


// jQuery AJAX call for JSON 
$.getJSON(Json, function (data){ 
    // Stick our user data array into a userlist variable in the global object 
    userListData = data; 
    // For each item in our JSON, add a table row and cells to the content string 
    $.each(data, function() { 
     //Get the date from MongoDB and convert to local format 
     var dt = new Date(this.DOB).toLocaleDateString(); 

     tableContent += '<tr>'; 
     tableContent += '<td><a href="#" class="linkshowuser" rel="' + this.username + '">' + this.username + '</a></td>'; 
     tableContent += '<td>' + this.email + '</td>'; 
     tableContent += '<td>' + dt + '</td>'; 
     tableContent += '<td><a href="#" class="linkdeleteuser" rel="' + this._id + '">delete</a></td>'; 
     tableContent += '<td><a href="#" class="linkupdateuser" rel="' + this._id + '">edit</a></td>'; 
     tableContent += '</tr>'; 
    }); 

    // Inject the whole content string into our existing HTML table 
    $('#userList table tbody').html(tableContent); 
}); 

};

回答

0

首先,你有一个错字你users.js 里面的意思是

var collection = db.get('userlist'); 
collection.find({ '_id': { $regex: reg }}, function (e, docs) { 
    res.json(docs); 
}); 

如果你想找到结果只有一个,你可以使用

// Of course, I assume you're using Mongoose 
var collection = db.get('userlist'); 
collection.findOne({ '_id': { $regex: reg }}, function (e, docs) { 
    res.json(docs); 
}); 

以及您的前端,你甚至不会给edit按钮一个链接

$.getJSON(Json, function (data){ 
    // Stick our user data array into a userlist variable in the global object 
    userListData = data; 
    // For each item in our JSON, add a table row and cells to the content string 
    $.each(data, function() { 
     //Get the date from MongoDB and convert to local format 
     var dt = new Date(this.DOB).toLocaleDateString(); 

     tableContent += '<tr>'; 
     tableContent += '<td><a href="#" class="linkshowuser" rel="' + this.username + '">' + this.username + '</a></td>'; 
     tableContent += '<td>' + this.email + '</td>'; 
     tableContent += '<td>' + dt + '</td>'; 
     tableContent += '<td><a href="#" class="linkdeleteuser" rel="' + this._id + '">delete</a></td>'; 
     tableContent += '<td><a href=/findOneUser/'+ this._id +' class="linkupdateuser" rel="' + this._id + '">edit</a></td>'; 
     tableContent += '</tr>'; 
    }); 

    // Inject the whole content string into our existing HTML table 
    $('#userList table tbody').html(tableContent); 
}); 
相关问题