findRecord() returns error but record is returned properly - ember.js

I have a strange error when calling data store's findRecord() function. Below is the function call inside of a route,
return this.get('store').findRecord('restaurant', params.restaurant_id);
And here are the errors I get,
vendor-6605726….js:10 Error while processing route: admin.restaurants.show e.getRecord is not a function TypeError: e.getRecord is not a function
vendor-6605726….js:8 TypeError: e.getRecord is not a function
The strangest thing is that the function works as it should since I can see using ember inspector that the query executes properly and returns the correct record. I have an index route that calls findAll() that does not throw any errors. I am formatting my json correctly I believe so I am out of ideas on what this could me.
Here is the json,
{"data":[{"type":"restaurants","id":1,"attributes":{"user_id":1,"name":"###########","address":"","phone":"##########","website":"##########","created_at":"2017-03-19 20:42:02","updated_at":"2017-03-19 20:42:02","description":"###########"}}]}

I recently had the same issue. It is highly likely that the JSON payload returned by your API isn't formatted correctly. Remember that findRecord() expects a single object to be returned, not an array:
{"data": {}}
not
{"data": []}
Double-check DevTools to see what your API returns to the client - make sure it's not an array.

Related

How to call window object method in Cypress?

I'm trying to call this method "window._e2e_cookie_popup_actions.acceptAll()" which basically sets the cookie policy in local storage.
When I call this method on browser console, it works perfectly.
However, when I try this on Cypress, it says "TypeError: Cannot read properties of undefined (reading 'acceptAll')"
This is how I tried on Cypress
cy.window().then((window)=>{
window._e2e_cookie_popup_actions.acceptAll()
})
Expection: If this method is called, the cookie should be set.
Thank you in advance!

read values from external json data file in postman

In my external jsondata file, i have defined “external” : null and I am calling that attribute in my post request body as “external”:{{external}} but acutally the “null”” value is not called. In the request body the value shows as “external”:{{external}} instead of “external”: null. please help me
https://github.com/postmanlabs/postman-app-support/issues/9606
It looks like a issue have raised above ticket , but as a work around use the below line of code in your pre-request code:
data["external"]===null?pm.variables.set("external",null):undefined
Here we are setting the value to null explicitly to null in local variable scope

What happens when ember's model hook's request fails

I have a typical scenario.
My model hook for Route-1 looks something like this.
model() {
return Ember.RSVP.hash({
posts: this.store.findAll('post'),
authors: this.store.findAll('author')
});
}
If I'm on Route-2 and navigate to Route-1 it will call the model hook.
And if I already have data on my store, both the findAll requests are resolved, triggering RSVP.hash to resolve.
But if the request fails, I'm getting undefined error in my console (chrome).(twice for each of findAll)
My error tracking system reports it as Unhandled promise error detected
the stack shows no relevant info either
defaultDispatch # ember.debug.js:18008
dispatchError # ember.debug.js:17987
onerrorDefault # ember.debug.js:31634
trigger # ember.debug.js:58713
(anonymous) # ember.debug.js:59614
invokeWithOnError # ember.debug.js:346
flush # ember.debug.js:405
flush # ember.debug.js:529
end # ember.debug.js:599
(anonymous) # ember.debug.js:1165
I am not able to figure out what is causing the error to be thrown because the promise findAll already got resolved. And ember tells me I have not handled the promise!
I tried putting catch/reject codes everywher but it never gets called. Because of course the promise was already resolved. So, it can not be rejected.
Then where is this error coming from!! I have no clue. There is no error till the adapter returns.
The only thing I could find was in my serializer normalizeFindAllResponse was not invoked whenever such failures happened.
Any help is greatly appreciated. Thanks!
I somehow solved this issue.
Before, in my adapter I was rejecting the promise with Promis' reason object.
Now, in case of error response, I am rather sending an object containing errors array, rather then failure reason object.
So, the object will get passed as payload to my normalizeFindAllResponse in serializer. There I check for existance of errors array in our payload parameter.
If there is such object then just return an empty object with data attribute set to empty array.
Note: Got the idea from here.

How to handle a 400 error with Ember Data (2.4.0-canary)

I can't figure out if my problem has to do with my query or my response or something else.
In my route I'm using a queryRecord:
model(params) {
return this.store.queryRecord('invitation', {
'invitation_token' : params.invitationToken
});
},
I'm properly receiving on the server side but I'm testing the case where the invitation token no longer exists. Therefore, the server is returning a 400 with a json payload which has an explanation of the error.
{"error":"Error finding invitation"}
But back on the ember side I'm just getting two errors (really it is just one).
Error while processing route: accept-invitation Ember Data Request GET /api/users/invitation returned a 400
Error: Ember Data Request GET /api/users/invitation returned a 400
It used to be (under Ember 2.2.1):
Error while processing route: accept-invitation Adapter operation failed Error: Adapter operation failed
Error: Adapter operation failed
What am I supposed to be returning from the server in the case that the token wasn't found? These messages makes it looks like something unexpected happen within Ember.
I'd assume that a 400 would tell Ember that there was an error, and it would go to the error state for the route. But instead it goes to the error state at the application level and spits out these error messages in the log.
I'm expecting the server to return a 400, so I'm not sure why Ember is complaining. What should I be returning and how should I properly handle this?
I believe that your application should be returning a response that is formatted differently. As per the JSON API specification,
Error objects MUST be returned as an array keyed by errors in the top
level of a JSON API document.
The specification also specifies that the following members, among others, may be provided (see the link for the full list).
title: a short, human-readable summary of the problem that SHOULD NOT
change from occurrence to occurrence of the problem, except for
purposes of localization.
detail: a human-readable explanation
specific to this occurrence of the problem. Like title, this field's
value can be localized
status: the HTTP status code applicable to this problem, expressed as
a string value.
If you try returning something along the lines of what is below, how does the tooling behave?
{
"errors": [
{
"status": "400",
"title": "Error finding invitation"
}
]
}

the persisting records instructions in the guide don't work for me

I'm trying to use the instructions find here:
http://emberjs.com/guides/models/persisting-records/
My server receives the json well and creates a record which it then returns properly, but my onSuccess function doesn't get anything usable as a response. It gets this strange object which if I try to pass onto the next route like the instructions say, it errors out saying this:
Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed '' (wrapped in (generated articles.view controller))
Here is my code:
https://github.com/mgenev/Full-Stack-JS-Boilerplate/blob/master/public/ember/controllers/articles_controller.js
I appreciate any help.
It looks like your transitionToArticle function requires an argument but you're not passing in anything when you're calling it:
article.save().then(transitionToArticle).catch(failure);