1

I have 2 objects, videos and views, in a one to many relationships, videos has many views and views belongs to videos. Videos has a view field which is a collection of all the views for that particular id.  

Every time a video plays, a view is recorded in the views table associated with the current video id. In order to have the view count I created this query.

SELECT COUNT(1) AS views
FROM views
WHERE video_id = '{{id}}' 

To retrieve the view count from the query in the component class I use this function to return the value:  

public getCount(id){
  let count ;
  this.backandService.getviews(id).subscribe(
    data => {
      count = data["0"].views
      console.log(count)
    },
  );
  return count
}

Now I want to update the viewcount column with the return value from the getCount function in order to do that:

public updateViews(id){
  this.backandService.update('videos', id, {views : this.viewcount}).subscribe();
}

But I keep getting the error:

Unexpected end of JSON input

Backand service functions to get all the view by id

public getViews(id){
        let encodedUrl =encodeURIComponent(JSON.stringify({"id":id}));
        return this.http.get(this.api_url + '/1/query/data/viewCount?
        parameters='+encodedUrl,{
                headers: this.authHeader  
        })
            .retry(3)
            .map(res => res.json());
    }

Backand Service Update function

 public update(object: string, id: string, item: any, deep: boolean = false, 
 returnObject: boolean = false) {
        let data: string = JSON.stringify(item);
        let query: string = '';
        if (returnObject){
            query += 'returnObject=true';
        }
        if (deep){
            query += query ? '&deep = true' : 'deep=true';
        }

        return this.http.put(this.api_url + '/1/objects/' + object + '/' + id + (query ? '?' + query : ''), data,
            {
                headers: this.authHeader
            })
            .retry(3)
            .map(res => res.json());           
    }
Sampath
  • 63,341
  • 64
  • 307
  • 441
Obed Lorisson
  • 449
  • 3
  • 8
  • 22

0 Answers0