node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

Non-ObjectId datatype on _id field

Open kuchta opened this issue 10 years ago • 3 comments

Is it possible to use schema with whose _id is not auto-generated ObjectID? If I use String, it doesn't seem to work, like if theres no route for e.g. /users/joe.

Thanks

M.

kuchta avatar Oct 29 '15 15:10 kuchta

Yeah, I think the assumption of ObjectID is baked into the code. It might be nice to get around this, but would require more thought than I have at the moment

baugarten avatar Nov 24 '15 15:11 baugarten

I got this working last week. I can't believe I erased the code. I can probably get it back together quickly. I think I kept :id as the route call (I did so because I come from WebApi 2 .net, and it is similar) and then created a function under the call to slice and return either the collection or single result.

baugarten - let me know if you would like me to get this code together for you, or if I missed the point here.

I even had it setup with nested routing for nested querying - i.e. /users/string:name/(optional)string:phone - then you can just take :name and :phone in your function and make the query work that way. It is like allowing a constrained query/reduce from the API which is a very valid function.

jump3r34 avatar Apr 08 '16 03:04 jump3r34

Ok, just to try and help - I found a hacky way I did this (since I am making the api call from inside my angular app - I can clean all data in and back out).

This is my users.js file which is inside of my routes directory and '/' in this file is localhost:3000/users, just for a point of reference.

// this is in the routes/users.js -- and is reachable by localhost:3000/users/firstname+lastname
var express = require('express');
var http = require('http');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('You made it to the users page');
});

router.get('/:fullName', function(req, res, next) {
  var fullName = req.params.fullName.split("+");
  var fName = fullName[0];
  var lName = fullName[1];
  res.send('This is pretty neat: ' + fName + ' ' + lName);
});

So, I took that concept and put it in my api.js routes and query data that way

jump3r34 avatar Apr 08 '16 04:04 jump3r34