Update embedded models in Loopback 4 - loopbackjs

I have the following model relationship in my LoopBack 4 application:
A Survey has 1-many relationship with Question,
Question has 1-many relationship with Option.
Am using MongoDB as the database. I am unable to figure out how to implement a PUT/PATCH REST api, that can update the entire Survey model containing the Questions and Options. I get the following exception when i try a PATCH on the sample request JSON below:
Exception: Unhandled error in PATCH /5be3e00aad0df83d10e580d1: 500
TypeError: Class constructor Question cannot be invoked without 'new'
at Function.DataAccessObject._coerce (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:1799:22)
at doUpdate (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:2735:20)
at C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:2714:11
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:178:5)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:176:15)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
at C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:2699:11
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:178:5)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:176:15)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
Sample PATCH request:
[{
"title": "Some survey title 1",
"description": "Some survey title 1",
"questions": [{
"options": [{
"description": "Some option description 1",
"type": "Option"
}],
"description": "Some question description 1"
}]
}]
Have not been able to find any helpful information on this yet. Any pointers please?
Thanks in advance.

Related

Question about Karate test case for POST method

I have an endpoint URL, within Swagger I must pass certain fields to test the POST method. I was wondering if anyone had an example of how to set up a Karate test for a POST method?
Yes, there are plenty in the documentation: https://github.com/intuit/karate
If you follow the quickstart, you will get a sample project with a working POST: https://github.com/intuit/karate#quickstart
Scenario: create a user and then get it by id
* def user =
"""
{
"name": "Test User",
"username": "testuser",
"email": "test#user.com",
"address": {
"street": "Has No Name",
"suite": "Apt. 123",
"city": "Electri",
"zipcode": "54321-6789"
}
}
"""
Given url 'https://jsonplaceholder.typicode.com/users'
And request user
When method post
Then status 201

Identify if the post author is a page or a user?

What is the proper way to identify if the author of a post is a user or a page?
v2.8/OrangeESP/feed?fields=id,from{id,name,link}
"data": [
{
"id": "118067454874491_1584993008181921",
"from": {
"id": "1112315252115029",
"name": "Clara Barranquero",
"link": "https://www.facebook.com/app_scoped_user_id/1112315252115029/"
}
},
I currently use the "from.link" for that and if it is of form
https://www.facebook.com/app_scoped_user_id// then is a User
if it's of from
https://www.facebook.com/ then it is a Page
In v2.4 I could have included from{category} in the response and if that was not empty it meant the author is a Page.
Include a field that´s only available in the Page or User table:
https://developers.facebook.com/docs/graph-api/reference/page/
https://developers.facebook.com/docs/graph-api/reference/user/
Checking for app_scoped_user_id in the link field is not a bad idea though.

Facebook Objects API Queries o

I have asked some old questions previously but those were quite misleading. So I decided to delete them and creating this one.
My object has a custom property named say it is portal content. In the Facebook Graph API Explorer data related to this object shown like;
{
"created_time": "2015-11-26T08:42:26+0000",
"title": "Title",
"type": "ns:type",
"data": {
"portalcontent": "portalcontent"
},
"id": "12515125125"
},
{
"created_time": "2015-11-26T08:04:09+0000",
"title": "Title2",
"type": "ns:type",
"id": "412512512512"
},
{
"created_time": "2015-11-25T12:56:03+0000",
"title": "Title3",
"type": "ns:type",
"id": "234124124124"
}
I am trying to query this data using Facebook graph api. But can not fetch just based on the portal content custom property.
So far I tried;
appid/objects?pretty=0&type=ns:type&limit=100&portalcontent=portalcontent
to do so. But it is still fetching all objects.
PS: please pm or comment on question for why you are downvoting it. Provide what else you need that I must put on the question. Downvoting for no reason getting people annoyed.
There's currently no way to filter results other than those described on the respective endpoint's docs.
See
https://developers.facebook.com/docs/graph-api/reference/application/objects/
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5

Using the Django API

First of all, any help with this would be amazing! I am currently trying to learn Django and am also fairly new to Python. I am trying to create a simple project management web service that could communicate using JSON with another application. All it needs to do is:
Give a list of all projects.
Give a list of all tasks for each of the projects.
Allow users to submit time for a project and task.
For models I currently have a 'Project' model and 'Task' model. I have filled the database with some dummy information. My project model has variables id, name, and description. Task just has project, task_name.
My view for task 1 above is
def api(request):
projects = Project.objects.all()#.order_by('id')
projects = serializers.serialize('json', projects, indent=2)
return HttpResponse(projects, mimetype='application/json')
associated url is url(r'^api/project/$', 'project.views.api'),
However this outputs the text below when typing
http://127.0.0.1:8000/api/project/
into the browser. Is there a better way to test JSON output? I would rather it only output the 'id' and 'name'. For some reason the id doesn't even show. This is what I put for id in models id = models.AutoField(primary_key=True). It works in the interactive shell.
[
{
"pk": 1,
"model": "project.project",
"fields": {
"name": "Project One",
"description": "This is the description for project one"
}
},
{
"pk": 2,
"model": "project.project",
"fields": {
"name": "Project Two",
"description": "This is Project Two"
}
}
]
I learn by example so if someone could show me how this should be done I would be extremely grateful!
By the way for outputting the list of tasks, I was thinking the url would be something like:
http://127.0.0.1:8000/api/project/?project_name
but I'm not sure how to handle that in the view.

real-time update page feed

here I am for real update "page", add fields to status to receive the "status" of
pages, I try to add as "link" fields to receive the subscription works but I do
not receive notification when I publish a link, it's really difficult to have
correct information
{
"object": "user",
"callback_url": "http://*/fbcallback.php",
"fields": [
"feed",
"link",
"status"
],
"active": true
}
http://bugs.developers.facebook.net/show_bug.cgi?id=18048#c40
As per the documentation at https://developers.facebook.com/docs/reference/api/realtime/ :
To setup a subscription, send a POST with the following parameters
It seems like you're posting a JSON object while you should do is a normal post with these variables (as if you're doing a with action="post").
Note, by the way, that the fields parameter needs to have a CSV value, so that would be "feed,link,status", and that there's no 'active' attribute.