so im Moment eine Anwendung mit MEAN-Stack. Das Problem, das ich im Moment habe, ist, wenn ich einen Aufruf an die API, bin ich in der Lage, erfolgreich alle Objekte und jedes Objekt nach ID aus der Datenbank (mit POSTMAN (Chrome)) Ich habe mit Mongoose & Express-Router eingerichtet. Meine Frage ist, kann ich ein Objekt nach seinem Namen abrufen? Ich habe im Internet gesucht und bin mir nicht sicher, wie ich das umsetzen könnte. Zum Beispiel: Dies ist der Api-Code, den ich derzeit habe.Holen Sie ein Objekt aus der Sammlung mit seinem Namen mit Hilfe von nodejs
var Dishes = require('../../app/models/dishes');
var Terms = require('../../app/models/terms');
var config = require('../../config');
module.exports = function(app,express){
// api ---------------------------------------------------------------------
var apiRouter = express.Router();
// middleware to use for all requests
apiRouter.use(function (req, res, next) {
// do logging
console.log('Somebody just came to our app!');
next();
});
// Test routes to make sure everything is working
//(accessed at GET http://localhost:3000/api)
apiRouter.get('/', function (req, res) {
res.json({message: 'Welcome to the API!'});
});
/** ================================= Dishes ==========================================**/
//on routes that end in /dishes , show all dishes in json
apiRouter.get('/dishes', function (req, res) {
Dishes.find(function (err, dishes) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(dishes); // return all dishes in JSON format
});
});
//on routes that end in /dishes/:_id , show all the this with the corresponding ID
// get the dish with that id
// (accessed at GET http://localhost:8080/api/dishes/:dish_id)
apiRouter.get('/dishes/:_id',function(req, res) {
Dishes.findById(req.params._id, function(err, dish) {
if (err) res.send(err);
// return that dish
res.json(dish);
});
});
return apiRouter;
};
Das Gericht Modell Ich bin Zugang ist wie folgt:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//with Mongoose everything is derived from a schema ! Lets get a reference and define our Dishes Schema
var DishSchema = mongoose.Schema({
dishName: {type: String, index: {unique: true}},
Desc : {type: String, index: { unique: true}},
Allergy: String,
HealthRisks: String
},{collection:'Dishes'});
module.exports = DishSchema;
//The Next step is to compile our schema into a model
var Dishes = mongoose.model('Dishes', DishSchema);//Dish Schema into model
// return the model
module.exports = mongoose.model('Dishes', DishSchema)
Was ich tun möchte, ist ein api Anruf zu (/ Geschirr /: dishName) und das entsprechende Gericht zurück. Jede Hilfe würde sehr geschätzt werden.
Leider habe ich schon versucht, diese vor und auf Abfrage von URL "http: // localhost: 3000/api/Geschirr/Eggs Benedict", das ist, was zurückgegeben wird: 'Code' { "message": " Cast to ObjectId fehlgeschlagen für Wert \ "Eggs Benedict \" bei Pfad \ "_ id \" ", " name ":" CastError ", " Art ":" ObjectId ", " Wert ":" Eggs Benedict ", "Pfad": "_id" } –
Überprüfen Sie die bearbeitete Antwort – RootHacker
Gleiches Ergebnis, es scheint sich auf den Pfad zu beziehen, der immer noch mit _id des Objekts verknüpft ist. "" message ":" Cast to ObjectId fehlgeschlagen für Wert \ "Eggs Benedict \" bei Pfad \ "_ id \" "? –