0

I am trying to retrieve a parse object with objectId in the show route on nodeJS. Below is my code to help you understand better.

//SHOW route

app.get("/books/:id", function(req, res) {
  var Books = Parse.Object.extend("Books");
  var query = new Parse.Query(Books);

  query.equalTo("objectId", req.params.id);
  query.find().then(function(foundBook){
    res.render("show", {book: foundBook});
  }, function(error) {
    res.send("Error: " + error.code + " " + error.message);
    });
});

Basically, The req.params.id does not return the objectID. when i try console.log(req.params.id), it returns the Title of the book stored in the database instead of the objectId which is important for linking to the /books/:id page.

Even when i try to retrieve all the objects from the database in the index route, i noticed that <%= book.get('objectId') %> is not displayed on the ejs page.

Please help me out of this. i am a beginner MEAN stack web developer but i am using parse server because the android and web applications would be sharing the same database on parse.com.

Thank You.

    <% books.forEach(function(book) { %>
        <div class="col-md-3 col-sm-6">
            <div class="thumbnail">
                <!-- this line of code gets the image content of the array and puts it in the img tag -->
                <img src="<%= book.get('coverPictureLink') %>"> 

            <div class="caption">
                    <h4><%= book.get('Title') %></h4>
            </div>

            <p>
                <!-- This code adds the button and links it to the ID of the campground that was clicked on!-->
                <a href="/books/<%= book.get('objectId') %>" class="btn btn-primary">More Info</a>
            </p>
        </div>
    </div>
     <% }); %>
</div>

Above is sample of the html page for displaying details of a particular book

felix
  • 73
  • 2
  • 8
  • Can you post book object sample, and html template you are rendering? – omurbek Aug 10 '18 at 11:34
  • What does your request look like on the URL? If your request looks something like ../books/SomeTitle, this is what you are seeing the title. If your request looks more like http//../books/1, your id should would be 1. Seems like your model and/or your endpoint definition should be cleaned up. – Dylan Wright Aug 10 '18 at 12:51
  • it's a show route. it looks like http//../books/:id . – felix Aug 10 '18 at 23:46
  • ParseObjectSubclass { className: 'Books', _objCount: 8, id: 'MDq4mlIIAh' } this is what the books object looks like when i console.log it by manually inserting the object id of a particular book on the query.find() method – felix Aug 10 '18 at 23:56
  • From your description it seems that you render your URLs incorrectly; the first commenter already asked for this: how does your _rendered_ URL look like? Is it `http//../books/xfg54s9hj` or is it `http//../books/The Title`? The `id` param has nothing to do with Parse per se, it's just a value that you interpret as a Parse Object ID. And it's defined at the location where the URL is rendered (e.g. your book list page). – dr_barto Aug 12 '18 at 15:42
  • i honestly don't know if the problem is the parse server API or nodeJS. i just know req.params.id which should always return the id of the object is not returning it. I have this same project done with mongoose and it worked well but now i am trying to use parse server, everything else seems to be working well expect that req.params,id does not return the objectId which is important for making reference to particular books/objects stored on the parse server – felix Aug 14 '18 at 15:14
  • i have also added a sample for the html page as @Omurbek requested. – felix Aug 14 '18 at 15:16

2 Answers2

0

I finally figured it out. The right way to retrieve object id on the html is book.id not book.get("objectId").

app.get("/books/:id", function(req, res) {
    //find the book with provided ID
    var Books = Parse.Object.extend("Books");
    var query = new Parse.Query(Books);

    query.get(req.params.id).then(function(book) {
      console.log('retrieved! ' + book.id);
      res.render('show', {book: book});
      }, function(error) {
          console.log('error occured');
          res.send('could not be retrieved');
    });
});

On the html file,

  <p>
                <a href="/books/<%= book.id %>">More Info</a>
  </p>
felix
  • 73
  • 2
  • 8
0

This is also the same if you are using node.js. with the parse server framework. Using .get('objectId') returns undefined values. Therefore you have to use.

for (i = 0; i < result.length; i++) {
  console.log('ID:' + result[i].id)
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Josh
  • 1
  • 1