最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add keyvalue pair to returned mongoose object - Stack Overflow

programmeradmin2浏览0评论

I have code that retrieves a Mongoose object and then uses the stripeCustomerId (stored in the document) to retrieve the Stripe customer object (via nodejs stripe). I then want to attach the stripe customer object to my Mongoose object.

exports.getPlatformByCId = (cId) => {
    return new Promise((resolve, reject) => {
        Platform.find({ clientId: cId }).then(response => {
            let user = response[0];
            stripe.customers.retrieve(user.stripeCustomerId, (err, stripeCust) => {                
                if(err) {
                    user["stripeCustomer"] = null;
                } else {
                    user["stripeCustomer"] = stripeCust;
                }
                resolve(user);
            })
        }).catch(err => {
            if(err) {
                if(err.error !== 'not_found') {
                    resolve(err);
                } else {
                    reject(err);
                }
            }
        })
    })
}

I also tried user.stripeCustomer = stripeCust I am getting the mongoose object back where it needs to go, but stripeCustomer is not a part of that object! I have verified that stripeCustis, in fact, returning the data I'm expecting it to.

Any guidance? I'm wondering if the Schema is somehow protected, and maybe there's a mongoose way to fix this?

I have code that retrieves a Mongoose object and then uses the stripeCustomerId (stored in the document) to retrieve the Stripe customer object (via nodejs stripe). I then want to attach the stripe customer object to my Mongoose object.

exports.getPlatformByCId = (cId) => {
    return new Promise((resolve, reject) => {
        Platform.find({ clientId: cId }).then(response => {
            let user = response[0];
            stripe.customers.retrieve(user.stripeCustomerId, (err, stripeCust) => {                
                if(err) {
                    user["stripeCustomer"] = null;
                } else {
                    user["stripeCustomer"] = stripeCust;
                }
                resolve(user);
            })
        }).catch(err => {
            if(err) {
                if(err.error !== 'not_found') {
                    resolve(err);
                } else {
                    reject(err);
                }
            }
        })
    })
}

I also tried user.stripeCustomer = stripeCust I am getting the mongoose object back where it needs to go, but stripeCustomer is not a part of that object! I have verified that stripeCustis, in fact, returning the data I'm expecting it to.

Any guidance? I'm wondering if the Schema is somehow protected, and maybe there's a mongoose way to fix this?

Share Improve this question asked Apr 5, 2018 at 1:33 Dan OrlovskyDan Orlovsky 1,0958 silver badges18 bronze badges 2
  • The user variable is just data. Is the user variable an Array or an Object? – Randy Casburn Commented Apr 5, 2018 at 1:48
  • The response is an array in which I assign user the first object in the response. I've verified user is, in fact, an object. – Dan Orlovsky Commented Apr 5, 2018 at 1:58
Add a comment  | 

1 Answer 1

Reset to default 18

Disconect the object from mongoose. When making the query use the lean() method of mongoose to get a JSON object. Then you can add keys to it.

http://mongoosejs.com/docs/api.html#query_Query-lean

发布评论

评论列表(0)

  1. 暂无评论