loopbackjs

Ensure unique field value in loopback model

会有一股神秘感。 提交于 2019-12-02 20:39:25
How to ensure uniqueness of a particular field in loopback model. Like below is the model Post, I have a field genericId in it, I want it to be unique in the database, and loopback to through an error, on duplicate key insertion. { "name": "Post", "plural": "Post", "base": "PersistedModel", "properties": { "genericId": { "type": "string", "required":True }, "moderatedAt": { "type": "date" } }, "validations": [], "acls": [], "methods": [] } I have tried searching there documentation, and other examples but no success. One solution which I can think of is, to create a remoteHook for the create

Loopback - Include a Relation's Computed Properties

巧了我就是萌 提交于 2019-12-02 12:55:53
问题 I have a loopback app and I'd like to be able to include computed properties from relations in an API call. For example, say I have an apartment model and an address model. The address has properties city and state . I'd like to make one call, to the apartment model, and include the city and state as a single string from the related address model. I took some inspiration from @Raymond Feng's answer to this question, and tried the following approach (excuse the coffeescript/pseudo-code):

Returning XML in response from Loopback Remote Method

折月煮酒 提交于 2019-12-02 08:50:35
问题 I am using the Loopback Connector REST (1.9.0) and have a remote method that returns XML: Foo.remoteMethod ( "getXml", { accepts: [ {arg: 'id', type: 'string', required: true } ], http: {path: '/:id/content', "verb": 'get'}, returns: {"type": "string", root:true}, rest: {"after": setContentType("text/xml") } } ) The response always returns an escaped JSON string: "<foo xmlns=\"bar\"/>" instead of <foo xmlns="bar"/> Note that the response does have the content-type set to text/xml. If I set my

Constantly getting 401 errors in loopback while using User Model

烂漫一生 提交于 2019-12-02 02:12:39
问题 I am new to loopback and I am not able to extend User Base model properly. Though in explorer it shows that it is extended but all API's give a 401 error. ex. In normal get call for /users I get.. { "error": { "name": "Error", "status": 401, "message": "Authorization Required", "statusCode": 401, "code": "AUTHORIZATION_REQUIRED", "stack": "Error: Authorization Required" } } I went thru all the links and questions but none of they are working for me. I have properly put public:true in model

Refresh page got 404: only happen when using /dist folder

北城余情 提交于 2019-12-02 02:07:50
问题 I have a LoopBackJS Restful server running at: localhost:3000. At front-end side, I have a simple Angular 2 app that consumes the above APIs. Everything is working fine when: Using nodemon to run the server (root folder at my-service/api) Using npm start to run the client side (root folder at my-service/client) I can do localhost:4200 to access my app which consumes the APIs running at localhost:3000 as mentioned above. Now I built the Angular app using 'ng build --prod' and pointed the

Refresh page got 404: only happen when using /dist folder

*爱你&永不变心* 提交于 2019-12-02 02:02:00
I have a LoopBackJS Restful server running at: localhost:3000. At front-end side, I have a simple Angular 2 app that consumes the above APIs. Everything is working fine when: Using nodemon to run the server (root folder at my-service/api) Using npm start to run the client side (root folder at my-service/client) I can do localhost:4200 to access my app which consumes the APIs running at localhost:3000 as mentioned above. Now I built the Angular app using 'ng build --prod' and pointed the output 'dist' folder inside the server directory (my-service/api/dist). I've configured the server code to

Aggregation issue with Mongo 3.6

删除回忆录丶 提交于 2019-12-02 00:11:55
I was using the aggregate function without any problem connecting a 3.4 mongodb. When I change to a 3.6 db, I've got the message: The 'cursor' option is required, except for aggregate with the explain argument. Sorry if it's already posted. I am unable to find any solutions In mongo 3.6 there has been changes while using aggregate you have to use cursor, unless you include the explain option, you must specify the cursor option. I faced same error as you were facing. Now you have to do it like this.aggregate( [ { $unwind : "$tags" }, {$group: {_id: '$tags', count: { $sum: 1} }}, {$sort: { count

Access request headers from beforeSave Model Hook

本秂侑毒 提交于 2019-12-01 17:28:11
How can i access the details of the user who raise the request from a Model Hook Comment.beforeSave = function(next,com) { //Want to add 2 more properties before saving com.added_at = new Date(); com.added_by = //How can i set the user id here ?? //In case of a Remote hook i have ctx in param and i can get user id like this ctx.req.accessToken.userId; But in Model Hook how can i do the same? next(); }; Is there any way to do this? I tried with Remote hook for main Item in the way MainItem.beforeRemote('**', function(ctx, user, next) { if(ctx.methodString == 'leave_request.prototype.__create_

Access request headers from beforeSave Model Hook

陌路散爱 提交于 2019-12-01 16:53:37
问题 How can i access the details of the user who raise the request from a Model Hook Comment.beforeSave = function(next,com) { //Want to add 2 more properties before saving com.added_at = new Date(); com.added_by = //How can i set the user id here ?? //In case of a Remote hook i have ctx in param and i can get user id like this ctx.req.accessToken.userId; But in Model Hook how can i do the same? next(); }; Is there any way to do this? I tried with Remote hook for main Item in the way MainItem

Save multiple models in loopback

你离开我真会死。 提交于 2019-12-01 14:38:20
I'm doing research on loopback and was wondering if it's possible to save to multiple models from one request. Say.. an Post has many Tags with many Images. A user form would have the following: Post Title Post Description Tag Names (A multi field. E.g.: ['Sci-Fi', 'Fiction', 'BestSeller'] Image File (Hoping to process the file uploaded to AWS, maybe with skipper-s3?) How would I be able to persist on multiple models like this? Is this something you do with a hook? You can create RemoteMethods in a Model, which can define parameters, so in your example you could create something like this in