UPDATE: The return statement is still not working as expected to show single user detail by id in the DAO
. I could only use for loop to iterate through the _id
to match the userId
, but when I click the edit
button for the number of user will show all previous user Id in the console.
Another problem is when I call this method in the Service class, the output is null
. Still crave for the solution to help me get over it.
@Override
public User get(Object userId) {
User user = new User();
FindIterable<Document> userTbl = database.getCollection("User").find();
for (Document doc : userTbl) {
String id = doc.getObjectId("_id").toString();
System.out.println("_id = " + id);
if (id.equals(userId)) {
return user;
}
}
return null;
}
edit user in Service class
public void editUser() throws ServletException, IOException {
Object userId = request.getParameter("id"); // get query string from the jsp
User user = userDAO.get(userId);
System.out.println("User full name is? " + user.getFullName());
}