1

I'm new to Node JS Develop. I have developed a specific REST service with Express and Mongo DB. Now I need to develop a rest service that when I pass some letter get an array of items that have these letters. I suppose that I could develop a GET Rest service using like operator. I have tried with this code but is wrong:

router.get('/tire/autocomplete/:size', VerifyToken, function(req,res){
var size=req.params.size;
TechInfo.find({ Size: new RegExp(size, 'i') }).toArray(err, techinfos) => {
    if (err) {
        console.log(err);
        return res.status(400).send({ status: 'ko', data: {msg: err.message }});
        console.log(err);
    }
    res.status(200).send({status: 'ok', data: {msg: 'Size tires available', tires :techinfos}});
});
});

and this my model Schema

var TechInfoSchema = new Schema({

"ID_code" :{type: Number, required: true},
"inches" : {type: Number, required: true},
"Brand" : {type: String, required: true},
"Size" : {type: String, required: true},
"type" : {type: String, required: true}
},{
collection: 'techInfo'
});

var TechInfo = db.model('TechInfo',TechInfoSchema)
module.exports = TechInfo;

When i call my rest service using postman i recieve this error

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>
<body>
    <pre>Cannot GET /api/v1.0/equipment/tires/autocoplete</pre>
</body>

There is some exaple that i can see about rest service to do this? Thanks

1 Answer 1

1

why are you converting the final result using find to an array ? it already is an array

router.get('/tire/autocomplete', VerifyToken, function(req,res){
var size=req.params.size;
TechInfo.find({ Size: new RegExp(size, 'i') },(err, techinfos) => {
    if (err) {
        console.log(err);
        return res.status(400).send({ status: 'ko', data: {msg: err.message }});
        console.log(err);
    }else{
    res.status(200).send({status: 'ok', data: {msg: 'Size tires available', tires :techinfos}});
    }

});
});

I just gave this answer as a rough idea. If this does not solve it update the post with your schema as well (model)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.