ga-wdi-boston / team-project

Other
2 stars 39 forks source link

Having trouble retrieving user_id for index action #321

Closed sstone72389 closed 7 years ago

sstone72389 commented 7 years ago

We are having trouble getting a curl script to work for retrieving the buyer id. Please see applicable code snippets/error message below.

CURL script:

#!/bin/bash

API="http://localhost:4741"
URL_PATH="/buyers"
TOKEN="YtgMq3RqixeMQN0uk0CDiTy6TVJzOCjg/SvRqZYytRg=--rbFPokOjKAlxDO/O4X9J0MP6T8HJWZHFzqUwCdiigRs="

curl "${API}${URL_PATH}" \
  --include \
  --request GET \
  --header "Authorization: Token token=$TOKEN" \
  --header "Content-Type: application/json" \

echo

index method in controller:

const index = (req, res, next) => {
  let owner = {_owner: req.currentUser._id };
  Buyer.find(owner)
  // Buyer.find({}).where(_owner).equals(req.body._owner)
    .then(buyers => res.json({
      buyers: buyers.map((e) =>
        e.toJSON({ virtuals: true, user: req.user })),
    }))
    .catch(next);
};

Error message from server:

{"error":{"message":"Cannot read property '_id' of undefined","error":{}}}
~/wdi/projects/print-shop/printshopbackend (development)
$ sh scripts/buyer-index.sh 
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Access-Control-Allow-Origin: http://localhost:7165
Vary: Origin
Content-Type: application/json; charset=utf-8
Content-Length: 74
ETag: W/"4a-33hcZwZp5Nbb9Cr6CscmEV+0bVM"
Date: Tue, 23 May 2017 19:02:29 GMT
Connection: keep-alive

{"error":{"message":"Cannot read property '_id' of undefined","error":{}}}
~/wdi/projects/print-shop/printshopbackend (development)

If we include authorization, the id should be retrievable. Any assistance would be greatly appreciated.

jordanallain commented 7 years ago

the request won't have a currentUser stored in it

laurpaik-zz commented 7 years ago

Look at your curl script syntax. Does anything look missing?

Maggicorp commented 7 years ago
#!/bin/bash

API="http://localhost:4741"
URL_PATH="/buyers"
TOKEN="f2k4AWSsRMij0o/wIDWVscfrB0WuddJr4MhygBeH59Y=--yLhNbRQYXfOVD1CEWZIgs+TvqKTmucYJpVXSAwhBYmg="

curl "${API}${URL_PATH}" \
  --include \
  --request GET \
  --header "Authorization: Token token=${TOKEN}" \
  --header "Content-Type: application/json" \

echo

We had the syntax wrong to reference the token and had to change the model to user

const index = (req, res, next) => {
  let owner = {_owner: req.user._id }
  Buyer.find(owner)
    .then(buyers => res.json({
      buyers: buyers.map((e) =>
        e.toJSON({ virtuals: true, user: req.user })),
    }))
    .catch(next);
};