Start a new topic

Trouble with modules.collectionAccess for a custom endpoint

Hey there,

I'm trying to create a custom endpoint that runs a series of mongo queries. I seem to have the pattern right, but for some reason I cannot pull (find()) objects out of my collection with a query. Here's what I've got:

function onRequest(request, response, modules){

// fetch all of our kickstart-tpls

modules.collectionAccess.collection('kickstarts-tpl').find({}, function(err, kickstartTemplates) {

// we need to convert the string to an ObjectId representation??? I've tried it without

// this and it still returns nothing.

var kickstartTemplateId = modules.collectionAccess.objectID(kickstartTemplates[0]._id);'kickstart_tpl_id: ' + kickstartTemplateId);

// "kickstart_tpl_id: 53220be7f811291203033885"

modules.collectionAccess.collection('topics-tpl').find({ "kickstart_tpl_id": kickstartTemplateId }, function(err, topicTemplates) {

response.body = topicTemplates; // [ ]





The screenshot of my "topics-tpl" collection, which clearly matches that "kickstart_tpl_id":

There must be something wrong with my Mongo query, but I'm just not too familiar yet. Appreciate the help!

Jeez, that code got butchered. Here it is in a gist instead:
The whole objectID component in Mongo can be a bit finicky, depending on how the data was written. For the line where you define kickstartTemplateId, try any of the following:

var kickstartTemplateId = modules.collectionAccess.objectID(kickstartTemplates[0]._id);

var kickstartTemplateId = kickstartTemplates[0]._id.toString();

var kickstartTemplateId = kickstartTemplates[0]._id;

var kickstartTemplateId = modules.collectionAccess.objectID(kickstartTemplates[0]._id.toString());

Let me know if the results change with any of these options.
Login or Signup to post a comment