Setting relations in Cloud Code

3 posts, 1 answers
  1. Nemr Nicolas
    Nemr Nicolas  avatar
    4 posts
    Member since:
    Jan 2014

    Posted 25 Mar 2014 Link to this post

    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. Answer
    Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 26 Mar 2014 Link to this post

    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.

     
  3. Nemr Nicolas
    Nemr Nicolas  avatar
    4 posts
    Member since:
    Jan 2014

    Posted 26 Mar 2014 in reply to Anton Dobrev Link to this post

    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
Back to Top