首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >rsvp.js和mongodb,将参数传递给承诺

rsvp.js和mongodb,将参数传递给承诺
EN

Stack Overflow用户
提问于 2015-07-15 09:18:36
回答 1查看 40关注 0票数 0

我有两个收藏,产品和类别。

我编写了一个名为getProductWithCategories的函数,它返回一个承诺,目前没有任何参数,不过理想情况下,我希望product_id是一个参数。

getProductWithCategories所做的是从product_id获得产品类别的产品。目前,让它工作的唯一方法是在函数之外初始化product_id。

该函数工作正常,但理想情况下,我希望该函数类似于:

代码语言:javascript
复制
var getProductWithCategories = function(product_id) {
   return new RSVP.Promise(function(resolve, reject) {

      // my mongodb search

   });
}

下面是我对这个程序的所有代码。

代码语言:javascript
复制
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);
        });
    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-15 10:15:23

如果使用“product”调用getProductWithCategories,这似乎很好。检查控制台,您将看到以下输出:

代码语言:javascript
复制
> prodid is defined here: spoon
> result: spoon

代码语言:javascript
复制
        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);
        });

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31426307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档