Google Calendar API NodeJS - specify fields - google-api-nodejs-client

Heyo, I'm using the google-api-nodejs-client package in my NodeJS project and I'm trying to build my query for calendar events. It's working, but I can't specify the "fields" field.
This returns five complete calendar events:
client.calendar.events.list({
calendarId: gcal_id,
maxResults: 5
})
.withAuthClient(authClient)
.execute(function(err, calendar) {
if (err)
console.log(err);
else
console.log(calendar);
});
But this returns nothing:
client.calendar.events.list({
calendarId: gcal_id,
maxResults: 5,
fields: {
items: ["end","start","status","summary"]
}
})
.withAuthClient(authClient)
.execute(function(err, calendar) {
if (err)
console.log(err);
else
console.log(calendar);
});
I've also tried:
fields: {
items: "end,start,status,summary"
}
Based on the Google Calendar V3 API, I should be able to make a query like this:
https://www.googleapis.com/calendar/v3/calendars/gcal_id.google.com/events?maxResults=5&fields=items(end%2Cstart%2Cstatus%2Csummary)&key={YOUR_API_KEY}
I'm not sure how to specify the "fields=items(end%2Cstart%2Cstatus%2Csummary)" portion of the URL, but I've tested it in Google's API Explorer and it works, so I know I'm doing something wrong in the JavaScript.
Any help would be much appreciated. For now I think I'll just clean up the response I get from the first code snippet.

Well, I figured it out. It should be one string.
fields: "items(end,start,status,summary)"

Related

mongoose query return empty array if filter has a field name that is camel cased

The following issue was with some code written in express/nodejs.
I experienced something I could not find any material in regards,
I encountered an issue whereas making a find query with mongoose returned an empty array.
After tinkering I discovered it was due to the key used (customerRef) being camel cased
I tried to tweak other fields from and to camelcase and got the same issue.
see example here:
const lastCart = await Cart.findOne({ customerRef: customer._id }, function(err, docs) {
if (err) {
console.log('TCL: err', err);
}
console.log('TCL: docs', docs); //result = []
});
then went and tweaked my database objects from:
{
....
"customerRef" : "5dfcd194ca19972b888d66c2",
....
}
to:
{
....
"customerref" : "5dfcd194ca19972b888d66c2",
....
}
then the query worked as expected:(customerRef >>> customerref)
const lastCart = await Cart.findOne({ customerref: customer._id }, function(err, docs) {
if (err) {
console.log('TCL: err', err);
}
console.log('TCL: docs', docs); //result is now ok = [...]
});
Now to top this off here's another bit of info:
I use studio 3T for mMongoDB and over there I do not have this issue when making queries.
I would appreciate any and all explanation on this behavior aswell as alternatives if possible.
See my full repo here:
(My skills are novice at best so I apologize in advanced and welcome any feedback)
https://github.com/Darkmift/MEAN-SuperMarket/blob/master/backend/app/controllers/userController.js#L121
So after some time I have concluded the issue was with my use of the mongo shell program (Studio 3t)
at the time I was not aware how to properly insert an ObjectId.
When using the built in query one must add:
{
...
"customerRef" : ObjectId(RefId)
...
}
and NOT:
{
...
"customerRef" : "RefId"
...
}

Express - return certain documents with named route parameters using axios

I'm having trouble communicating between the frontend and backend for a selected GET request.
I am using a React frontend with an express/mongoose setup out in the backend.
In the frontend, I do a GET call using axios for:
axios.get('/api/orders/', {
params : {
name: this.props.user.name // user name can be Bob
}
})
And in the backend I'm having a hard time understanding the correct method I would need to do to query the database (example below doesn't work). I found stuff with .select but even then I still can't get it to work:
router.get('/orders', function(req, res) {
Order.find({}).select(req.params).then(function (order) {
res.send(req.params);
})
});
I also tried doing this to see if I can even get the params to send properly and to no demise:
router.get('/orders/:name', function(req, res) {
res.send('client sent :',req.query.name);
});
The orders document model holds objects that house an ordered array and a name (type: String) attached to the object. The Mongoose scheme for the order:
const orderScheme = new Schema({
name : { type : String },
orders : { type : Array}
});
In my MongoDB, I can see all the "Master Orders" send back. Each master order has the name of who submitted it, plus all the orders within (there can be a ton of orders).
What I'm trying to exactly do is pull up all orders that have a certain name. So if I search "TestAccount", I'll get all of bob's orders. I've included an image below:
Any pointers?
Client-side:
axios.get('/api/orders/' + this.props.user.name)
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
You need to handle the Promise when resolved/rejected.
Server-side:
router.get('/orders/:name', function(req, res) {
return Order.find({name: req.params.name}).then(function(orders) {
// return orders when resolved
res.send(orders);
})
.catch(function (error) {
// handle error
console.log(error);
})
});
You did not specify a named route parameter in your route path.
You also aren't accessing the name property by using req.params only.
You should use Model.find() conditions parameter to specify which document[s] you're trying to find. Query.prototype.select() is for filtering document fields.

Rally: Date User Story is Defined

I am writing a program that needs to fetch user stories that are defined before day 3 of a release. Is there a way to find out what day a user story was set to the 'defined' state so that I could query that?
I looked through the Web Service API docs but I couldn't find anything that could help me, although I could have missed something.
This is the code I am using to get the 3rd day of a release:
var releaseStart = combobox.getRecord().get('ReleaseStartDate');
releaseStart.setDate(releaseStart.getDate()+3);
this._startDate = Rally.util.DateTime.toIsoString(releaseStart);
But I'm not sure how to relate this to the date a user story is defined.
If anyone could help it would be much appreciated!
You're right- this piece of data does not exist in the standard WSAPI. You can get it from LookbackAPI however. How about something like this to get started?
var releaseStart = combobox.getRecord().get('ReleaseStartDate');
var startDate = Rally.util.DateTime.add(releaseStart, 'day', 3);
var snapshotStore = Ext.create('Rally.data.lookback.SnapshotStore', {
context: {
workspace: this.getContext().getWorkspaceRef()
},
find: {
_ProjectHierarchy: this.getContext().getProject().ObjectID,
_TypeHierarchy: 'HierarchicalRequirement',
ScheduleState: {$gte: 'Defined'},
__At: startDate
},
sort: {
_UnformattedID: 1
},
fetch: ['FormattedID', 'Name', 'ScheduleState'],
limit: Infinity,
autoLoad: true,
listeners: {
load: function(store, records) {
//TODO: work with records here
}
}
});
More information on working with the Lookback API is here: https://help.rallydev.com/apps/2.1/doc/#!/guide/lookback_api

Express JS routing based on the user names

I am working on a express js project.I have got all my basic routing set up working perfectly. Usually when I want to search a record based on id I do this:
router.route('/sensors_home/:sensor_id')
.get(function (req, res) {
Sensor.findById(req.params.sensor_id,function(err, sensorInfo) {
if (err)
res.send(err);
res.send(sensorInfo);
});
});
This allows me to retrieve the data when I do http://localhost:4000/sesnors_home/45000cbsfdhjbnabfbajhdb
(45000cbsfdhjbnabfbajhdb = Object id from the MongoDB )
Now my goal is to have several users to my application. I have my mongoose schema set up and the mongoDB looks like this :
Here is the issue: I wanna retrieve data corresponding to John Peterson based on his _id that is "John".Instead of doing this http://localhost:4000/sesnors_home/45000cbsfdhjbnabfbajhdb I wanna do something like this http://localhost:4000/sesnors_home/John and retrieve all the data specific to John. I tried various methods but still stuck with this issue. I tried using req.params._id and also some Mongodb queries on the User Collection but still no luck. Please suggest some ideas.
Thanks!
UPDATE:
I tried using the following code :
router.route('/sensors_home/:id')
.get(function (req, res) {
res.send(_id.toString());
User.findOne({_id: req.params._id} ,function(err, sensorInfo) {
if (err)
res.send(err);
res.send(sensorInfo);
});
});
This gives me the following error :
ReferenceError: _id is not defined
Have you tried the following?
router.route('/sensors_home/:_id')
.get(function (req, res) {
Sensor.findOne({_id: req.params._id},function(err, sensorInfo) {
if (err)
res.send(err);
res.send(sensorInfo);
});
});

WaterlineJs find() with no criteria and fields/select provided does not work

I am trying to fetch all the records but with selected fields, I have tried the following ways but none works:
Post.find(
{
where: {},
select: ['title']
}
);
Post.find(
{},
{
fields: {
title: 1
}
}
);
As this answer points out, the fields param "WILL work as long as you pass other params with it such as limit or order."
Alternatively, if you want this throughout your application, you could define a custom toJSON function for your model, under attributes. If not, you could still define it under some other (e.g. filter) and use map to return the custom objects instead of the default model. Remember to take care of the control flow while using map though. Use async/promises/raw logic to avoid returning before all objects are processed.
The issue has been resolved in sails-mongo latest version:
https://github.com/balderdashy/waterline/issues/1098
Thanks
I've played with trying to get above answer to use limit or order to kick in the projection to no avail.
I did see this in the docs located here:
http://sailsjs.org/documentation/reference/waterline-orm/models/native
With an out of the box solution for exactly what you're doing (pasted here for ease of use).
Pet.native(function(err, collection) {
if (err) return res.serverError(err);
collection.find({}, {
name: true
}).toArray(function (err, results) {
if (err) return res.serverError(err);
return res.ok(results);
});
});
Swap out the response base things and change Pet to Post and, this ought to work in the sails console:
Post.native(function(err, collection) {
if (err) throw new Error(err);
collection.find({}, {
title: true
}).toArray(function (err, results) {
if (err) throw new Error(err);
console.log(results);
});
});
You'll still get the _id field, and if you don't want that then hit the Mongo docs on not getting those hint(title: true, _id: false)hint
Hope this helps!