1

I can do a console log and get company. It has _id as an object property.

$scope.add2 = function () {
  if (!$scope.company2) $scope.company2 = [];

  var company2 = new Companies({
    name: 'FestivalSauce',
    store: 'DePereYo',
  });

  company2.$save(function (response) {
    $scope.company2.push(response);
  });

  console.log(company2);       
};

This is what I get in console

Resource {name: "FestivalSauce", store: "DePereYo", toJSON: function, $get: function, $save: function…}
$promise: undefined
$resolved: true
__v: 0
_id: "5462f632b85133000023c1ba"
name: "FestivalSauce"
store: Array[1]
__proto__: Resource

If I change console.log(company2._id) I get 'undefined' in console. Doesn't make sense.

Update: Here's the proto https://i.stack.imgur.com/D4aof.png

UX Guy
  • 257
  • 2
  • 13
  • It looks like the name of the property is `0_id` not `_id` – Dmitry Nov 12 '14 at 06:06
  • Thanks Dmitry! I recognize your name :) I checked at 0 is just the value for _v. – UX Guy Nov 12 '14 at 06:10
  • I updated the question to reflect this. – UX Guy Nov 12 '14 at 06:12
  • Can you show the code for its prototype the `Resource`, and the constructor `Companies`? Usually `_` is used to denote that the property is private. Maybe they used some new ES6 features to make it inaccessible. It is hard to tell without seeing the code. – Dmitry Nov 12 '14 at 06:21
  • Sure thing. There's the option to expand even more text in some of these. Let me know if you need more details. – UX Guy Nov 12 '14 at 06:54
  • Okay, I added an image http://i.imgur.com/ecja6ev.png – UX Guy Nov 12 '14 at 06:58
  • `company2.$save()` is asynchronous, and will create the `._id` property only after you try to log it. – Bergi Nov 13 '14 at 01:53

2 Answers2

2

I think the issue is that company2 is actually an array. It's confusing because you're mixing objects and arrays. I bet if you try console.log(company2[0]._id), it will display 5462f632b85133000023c1ba.

I'd be interested to see the definition of Companies to confirm.

Drew Fyock
  • 103
  • 4
  • Oh man, this kept me from being stuck forever. The find() method in Mongoose and MongoDB returns an array, even if there is just one record. Duh. I just oversaw the array brackets in the JSON output. – Mark Langer May 30 '17 at 12:45
0

company2 doesn't seem to be a plain object with _id as its property. You can however try converting it to JSON. You can try console.log(company2.toJSON()._id)

Munim
  • 6,310
  • 2
  • 35
  • 44
  • Thanks for your response, Munim! It's still saying undefined when I add toJSON() in there. In my brackets IDE text editor, it auto suggests _id for company2._id, but it just shows up as undefined in the browser console for some reason. – UX Guy Nov 12 '14 at 06:52
  • I expanded the __proto__ if that helps solve the question with more information http://i.imgur.com/ecja6ev.png – UX Guy Nov 12 '14 at 06:59