Firstly, you need to make sure that both files are included in the HTML page that you are loading (i.e. your form).
Next, I see that your test.js file is simply a JSON object.
I would recommend changing this so that you return a JSON object via a function call rather. This will allow you to pass through your gathered inputs into the function to return your required JSON object to send to Mongo.
I would recommend giving the following changes a try:
sandbox.js changes
function myTime() {
var result = {}
result.startValue = document.getElementById("startTime").value;
result.endValue = document.getElementById("endTime").value;
result.intervalValue = document.getElementById("interval").value;
result.startDate = new Date("1/1/2015 " + startValue);
result.endDate = new Date("1/1/2015 " + endValue);
result.offset = intervalValue * 1000 * 60;
return result;
}
This will allow you get get all the variables into a result to pass through to the test.js file
test.js changes
function getMongoObject(values) {
return { "$match" : { "datetime" : { "$gt" : values.startDate, "$lt" : values.endDate } } },
{
"$group": {
"_id":{
"$add": [
{ "$subtract": [
{ "$subtract": [ "$datetime", new Date(0) ] },
{ "$mod": [
{ "$subtract": ["$datetime" , new Date(0) ] },
values.offset]}] },
new Date(0)
]},
"Humidity": {$avg: "$humidity"},
"Temperature": {$avg: "$temperature"}
},
},
{ "$project" : { "_id" : 1 , "Humidity" : 1, "Temperature": 1 } },
// { "$limit" : 10 },
{ "$sort" : {"_id":1, "Humidity":1, "Temperature": 1}}
}
Once you've made those changes to the test.js file, you can execute the following where you want to get the JSON object to pass to Mongo.
var mongoValues = getTime();
var mongoObject = getMongoObject(mongoValues);
Now you can use mongoObject to send to the DB.
UPDATE:
Approach 2:
If you do not want to send the variables through to the test.js file you will need to make your variables in the sandbox.js file global. Currently they are "private" (scoped only to the function)
Try these changes to sandbox.js
var startValue, endValue, intervalValue, startDate, endDate, offset;
function myTime() {
startValue = document.getElementById("startTime").value;
endValue = document.getElementById("endTime").value;
intervalValue = document.getElementById("interval").value;
startDate = new Date("1/1/2015 " + startValue);
endDate = new Date("1/1/2015 " + endValue);
offset = intervalValue * 1000 * 60;
}
In my opinion, option 1 is still better because it allows you to control what the variables are that you are binding to your JSON object that you will be passing to Mongo.
With this method, you could accidentally get different results in your JSON object due to the variables not being set at the time test.js is invoked or due to the variables being accidentally overridden.
UPDATE 3
In order to access the startDate and other variables in the test.js file you will need to make the JSON object a variable.
I have made a github repo for a test to show you want I mean, check it out here:
https://github.com/NewteqDeveloper/so-q-41462690
From this example you will note that the alert displays 2. (startdate + 1).
For more information check this stackoverflow page: Can I access variables from another file?.
An important note to make is that you need to include the sandbox.js file before the test.js file in the html page.