3

Hi when i post to deployd i get this strange error in console

Object {name: "MongoError", message: "key $$hashKey must not start with '$'", status: 400}

code

 dpd.timesheetsdone.post({
      "projectId": $scope.projectId ,
      "formandSig":  $scope.signature,
      "timesheets": $scope.timesheets

    }, function (result, err) {
      if (err) return console.log(err);
      console.log(result, result.id);
    });

project id and signature is a simple string, timesheets is an array

if i replace scope.timesheets with

  [
    {
        "projectId": "1000",
        "date": "2015-05-15T22:00:00.000Z",
        "start": "25200"
    }
]

it works..

onsole.log(scope.timesheet... returns an object with same values + and hash key

Stweet
  • 683
  • 3
  • 11
  • 26

2 Answers2

3

Angular automatically adds $$hashKey to all objects within your $scope.timesheets array. You can get rid of these by doing angular.toJson($scope.timesheets)

so your post would look like this:

 dpd.timesheetsdone.post({
  "projectId": $scope.projectId ,
  "formandSig":  $scope.signature,
  "timesheets": angular.toJson($scope.timesheets)
  ...
Dan Moldovan
  • 3,576
  • 2
  • 13
  • 24
  • seems to work but now i get a new error Object {message: "Cannot call method 'forEach' of undefined", status: 400} on my deployd i got this.timesheets.forEach.... on post – Stweet May 25 '15 at 14:14
  • You have to decode JSON back by using ```JSON.parse(this.timesheets)``` on your Deployd code – Dan Moldovan May 25 '15 at 14:15
  • mm this gives me Object {message: "Unexpected token u", status: 400} – Stweet May 25 '15 at 14:18
2

Removed hash key parseing timesheets to JSON Dont know if this is the right / best way to remove hash keys?

      $scope.sign = function() {
   var sheets = angular.toJson($scope.timesheets);
   var sheets = JSON.parse(sheets);
    dpd.timesheetsdone.post({
      "projectId": $scope.projectId ,
      "formandSig":  $scope.signature,
      "timesheets": sheets

    }, function (result, err) {
      if (err) return console.log(err);
      console.log(result, result.id);
    });
  }
Stweet
  • 683
  • 3
  • 11
  • 26