简体   繁体   中英

rsvp.js and mongodb, passing arguments into promises

I have 2 collections product and category.

I have written a function called getProductWithCategories which returns a promise and at the moment does not have any parameters although ideally I would like product_id to be a parameter.

What getProductWithCategories does is it gets the product with the product categories from the product_id. At the moment the only way I can get it to work is by initializing product_id outside the function.

The function works ok, but ideally I would like the function to be something like:

var getProductWithCategories = function(product_id) {
   return new RSVP.Promise(function(resolve, reject) {

      // my mongodb search

   });
}

Below is all my code for this program.

MongoClient = require('mongodb').MongoClient;
    RSVP = require('rsvp');

    MongoClient.connect('mongodb://localhost:27017/mydatabase', function(err, db) {

        if (!err) {
            console.log("Database is connected");
        }

        var product = db.collection('product');
        var category = db.collection('category');
        var product_category = db.collection('product_category');

        product.insert({_id: 'knife', madeof: 'silver'});
        product.insert({_id: 'spoon', madeof: 'silver'});
        product.insert({_id: 'car', type: 'hatchback'});
        product.insert({_id: 'van', type: 'transit'});
        product.insert({_id: 'lorry', type: '4ton'});
        product.insert({_id: 'beer', type: 'lager'});
        product.insert({_id: 'whisky', make: 'Southern comfort'});
        product.insert({_id: 'wine', madein: 'france'});

        category.insert({_id: 'cutlery'});
        category.insert({_id: 'transport'});
        category.insert({_id: 'alcohol'});

        product_category.insert({category_id: 'cutlery', product_id: 'knife'});
        product_category.insert({category_id: 'cutlery', product_id: 'spoon'});
        product_category.insert({category_id: 'transport', product_id: 'car'});
        product_category.insert({category_id: 'transport', product_id: 'van'});
        product_category.insert({category_id: 'alcohol', product_id: 'beer'});
        product_category.insert({category_id: 'alcohol', product_id: 'whisky'});
        product_category.insert({category_id: 'alcohol', product_id: 'wine'});

        // product_id needs to be set before calling this function
        var getProductWithCategories = function() {
            return new RSVP.Promise(function(resolve, reject) {
                product.findOne({"_id": product_id}, function(err, product) {
                    if (err) {
                        reject(err);
                    }
                    product_category.find({"product_id": product._id}).toArray(function(err, docs) {
                        if (err) {
                            reject(err);
                        }
                        var categories = [];
                        docs.forEach(function(doc) {
                            categories.push(doc.category_id);
                            product['categories'] = categories;
                            result = product
                            // console.log(result);
                            resolve(result);
                        });
                    });
                });
            });
        }

        var product_id = 'spoon'; // product_id is set to 'spoon' before calling getProductWithCategories
        getProductWithCategories().then(function() {
            console.log(result);
        }).catch(function(error) {
            console.log('something went wrong', error);
        });
    });

This seems to work fine if you call getProductWithCategories with 'product_id'. Check the console and you'll see this output:

> prodid is defined here: spoon
> result: spoon

  var getProductWithCategories = function(prodid) { return new Promise(function(resolve, reject) { console.log('prodid is defined here: ' + prodid); if (prodid) { resolve(prodid); } else { reject('not defined'); } }); } var product_id = 'spoon'; getProductWithCategories(product_id).then(function(result) { console.log('result: ' + result); }).catch(function(error) { console.log('something went wrong', error); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM