0

I'm applying a function to map a calculation over a list of dates. In the old calculation I just manually entered each date and ran the code. As part of if that code I would use .get('year).getInfo() to obtain the year from a manually entered data.

var startDate = ee.Date('2016-10-01');
var endDate = ee.Date(startDate.advance(1,'month'));

var startyear = startDate.get('year').getInfo(); 
var endyear = startDate.get('year').getInfo();

I've tried do this again within my function

  var start = ee.Date(date);
  var end = ee.Date(date).advance(1,'month');
  var date_range = ee.DateRange(start,end);
  
  var startyear = start.get('year').getInfo(); 
  var endyear = start.start.get('year').getInfo();

But I'm getting the error Unknown variable references: [].

I'm guessing it's got something to do with the .getInfo function and client vs server side. However, to obtain year as an object for use, I need to use .getInfo to obtain it. Does anyone know any work arounds?

The link to my code is here

2
  • Why do you need to get the years with .getInfo()?
    – JonasV
    Feb 2, 2021 at 9:22
  • I have another line code further down that uses the year, var startdate = ee.Date.fromYMD(startyear, 1, 1); I could only get that to read the year using .getInfo. Are there other options? Feb 2, 2021 at 9:28

1 Answer 1

1

Your issue is in this line:

var enddate = ee.Date.fromYMD(endyear + 1 , 1 , 1);

because endyear is an ee.Number if you don't use .getInfo() you can not call +1 on it. Instead you need to use the Earth Engine specific functions:

var enddate = ee.Date.fromYMD(endyear.add(1) , 1 , 1);

If you do this, you can just remove the .getInfo() calls.

1
  • Thanks I thought it might be something simple. Greatly appreciated. Feb 2, 2021 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.