This is a migrated thread and some comments may be shown as answers.

Setting relations in Cloud Code

2 Answers 158 Views
Cloud Code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nemr Nicolas
Top achievements
Rank 1
Nemr Nicolas asked on 25 Mar 2014, 06:18 PM
Hi,

I've been trying to figure out how to set a relation within the beforeCreate Event to set relational fields within my Backend.

An Example:

- Two tables: Offers and Companies
- Offers contains a field with a relation to Companies
- Offers contains another field which entry corresponds to a unique entry within Companies (e.g Website)
- The client side App will now insert a new Offer providing the Website and further informations
- The Cloud Code should now find the entry in Companies which corresponds with the provided Website
- The Cloud Code uses the result to set the relation within the Offers table (for sure in this case the Id of the Companies table)

I figured out how to find the corresponding Companies entry using following within the beforeCreate Event:

  var filter = { 'Website' : request.data.Website};
  Everlive.Sdk.$.data('Companies').get(filter, 
                                      function(error, data) 
                                      {
                                        if (error) 
                                        {
                                          // do error handling
                                        }
                                        else
                                        {
                                          request.data.Company = data.result.???;
                                        }
                                        
                                      });

This code returns the data result containing all information about the Companies entry but I'm not sure how to access the Id to add it to my request object using the lines request.data.Company = data.result.???
Using a hardcoded version works but makes no sense ;) (e.g. request.data.Company = "669068d0-b445-11e3-bf0b-e3a3bc3e3d68"

I also tried using the new EverliveSDK.Query() object getting an error like: Query() is not a function of ...

Furthermore I would be interested how to use query filters in Cloud Code to minimize the query results to specific fields.

Any help is welcomed and maybe I just miss the forrest for the trees.

Thanks  and regards

2 Answers, 1 is accepted

Sort by
0
Accepted
Anton Dobrev
Telerik team
answered on 26 Mar 2014, 09:31 AM
Hi,

Please, refer to the sample code below. It is using the Everlive.Query() object with a projection of the desired fields to be returned. The query object is initialized with a slightly different syntax within the cloud code.

Everlive.Events.beforeCreate(function (request, context, done) {
    //  console.log(JSON.stringify(request));
 
    var query = new Everlive.Sdk.Query();
    query.where().eq("Website", request.data.Website).done().
        select("Other field name", "Some other field name"); // the Id is always returned
 
    var data = Everlive.Sdk.$.data("Companies");
    data.get(query, function (err, items) {
        if (err) {
            // handle the error
            done();
        } else {
            request.data.CompanyId = items.result[0].Id;
            done();
        }
    });
});

Let us know if you have further questions.

Best regards,
Anton Dobrev
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
0
Nemr Nicolas
Top achievements
Rank 1
answered on 26 Mar 2014, 11:36 AM
Hi Anton,

thanks for your reply, works as expected. Tried a bunch of syntax variations before but I think the data.result[0].Id (especially the index value) did the trick.

Regards
Tags
Cloud Code
Asked by
Nemr Nicolas
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Nemr Nicolas
Top achievements
Rank 1
Share this question
or