EzDevInfo.com

nstore

nStore is a simple, in-process key/value database for node.js

How to get $resource key to pass to a view in AngularJS?

I need to get the resource key from an array of resources in my AngularJS SPA. I use a resource like this:

var answers = $resource('/api/answers/:id', {id: '@id'})

It connects to my node.js backend which loads JSON from nstore. The "API" is simply getting data out of a nStore db, I just do a res.end(result) sending my result like this:

{
  "1moabd0c": {
    "q1": "asdf",
    "q2": "3",
    "q3": [
      "false",
      "true"
    ],
    "q4": "a@a.com"
  },
  "2loe5p5p": {
    "q1": "edsds",
    "q2": "3",
    "q3": [
      "false",
      "true"
    ],
    "q4": "sdfwe@mai.com"
  }
}

When I do give a parameter (like, calling "/api/answers/1moabd0c" from that first object), I get only that one result, like this:

{
  "1moabd0c": {
    "q1": "asdf",
    "q2": "3",
    "q3": [
      "false",
      "true"
    ],
    "q4": "a@a.com"
  }
}

So how do I extract that object key?

I use the resources in a view:

tbody
      tr(ng-repeat='answer in answers')
        td {{answer.q4}}
        td
          a(rel='nofollow' href='#/answers/{{answer.key}}') Open

But it does not work. I tried using {{asnwer.key}}, {{answer.id}} there...what else can I use to get that hash?

Edit: when I do this in my controller:

  $scope.answers = Answers.query()
  window.x = $scope.answers;

And then in the window I log out x, I get an array, but that array has $$hashKey props. My guess those are angulars' own hash keys. I need the original ones. The array looks like this (edited it a bit to fit here nicely)

[
    {
        "$$hashKey": "005",
        "q1": "asdf",
        "q2": "3",
        "q3": [
            "false",
            "true"
        ],
        "q4": "a@a.com",
        __proto__: g /* Which seems to be the angular resource object literal, with $delete, $get and stuff */,
    },
    {
        "$$hashKey": "009",
        "q1": "Ovojejedanodgovor",
        "q2": "4",
        "q3": [
            "false",
            "true"
        ],
        "q4": "babaa@a.com",
        __proto__: g /* again this obj */
    }
]

Source: (StackOverflow)