2017-08-08 1 views
0

こんにちは、私はnodejsを初めて使い、編集ボタンをクリックすると新しいページにリダイレクトして1つのレコードを表示しようとしています。私はルートがどのように働くか分かりません。 私が参照する場合: http://localhost:1337/私はそこにすべてのユーザーとテーブルを参照してください。私は編集を言う列を追加しましたが、編集をクリックすると、findsoneレコードがpugページを開く関数を実行しようとしています: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); 
}); 

であることを意味しそして、あなたは1つの結果だけを検索する場合、あなたは

// 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); 
}); 
関連する問題