2

I have this Javascript object

var output = { 
   "_resolved": true, 
   "_result": [ { 
                   "title": "Pencil", 
                   "quantity": 1, 
                   "objectId": "HknL2ZAspb" 
              } ] 
}

When I try to get the value of title like this:

output._result[0].title

it returns undefined.

However

output._result[0]

correctly returns

{ "title": "Pencil", "quantity": 1, "objectId": "HknL2ZAspb" }

Any idea why output._result[0].title returns undefined? I also tried output["_result"][0]["title"] but it returns undefined too.

var output = { 
   "_resolved": true, 
   "_result": [ { 
                   "title": "Pencil", 
                   "quantity": 1, 
                   "objectId": "HknL2ZAspb" 
              } ] 
}

console.log(output._result[0].title);

Serverside code (Node):

const productsQuery = new Parse.Query(ProductTable);

output = productsQuery.get('HknL2ZAspb');   //objectID

app.get('/', (req, res) => res.send(JSON.stringify(output["_result"][0]["title"])))

Note the Query and other code is just fine. As output returns the exact object.

20
  • 4
    output._result[0].title works correctly with the data you provided. You must have a typo in your code. Commented May 4, 2018 at 0:16
  • 1
    Show your code related to your post. Commented May 4, 2018 at 0:21
  • 1
    @Daman please add full code of app.get handler body Commented May 4, 2018 at 0:24
  • 1
    Isn't the parse query asynchronous? Commented May 4, 2018 at 0:32
  • 1
    @pfg it says - object Commented May 4, 2018 at 0:37

1 Answer 1

1

Since You're using Parse and it's asynchronous thing.

From examples in official documentation I can give working example:

const ProductTable = 'products';

app.get('/products/:id', async (req, res) => {
  try {
    const productId = req.params.id;
    const query = (new Parse.Query(ProductTable))
                    .equalTo('objectId', productId);
    const result = await query.find();

    if (result[0]) {
      return res.status(200).send(result[0]);
    }
    res.status(404).send({});
  }
  catch (error) {
    res.status(500).send(error);
  }
});

Use it like: http://127.0.0.1:3000/products/HknL2ZAspb

Sign up to request clarification or add additional context in comments.

3 Comments

I do. because when i output output._result it does output an Object
Why does everything return the right values though? Could all the things we've tried been called later after everything has been downloaded?
@pfg Daman was not giving whole code, + I did not found any mention of .get to query object. Maybe it's some abstraction over Parse class. So I've just wrote proper example depending on official manual. In another hand result and resolved may be special fields of custom promise object that is being logged to console or stringified on demand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.