9

我有检索 Mongoose 对象的代码,然后使用stripeCustomerId(存储在文档中)检索 Stripecustomer对象(通过 nodejs 条带)。然后我想将条纹customer对象附加到我的 Mongoose 对象上。

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);
                }
            }
        })
    })
}

我还尝试user.stripeCustomer = stripeCust将猫鼬对象放回需要去的地方,但stripeCustomer不是该对象的一部分!我已经证实,stripeCust事实上,它返回了我期望的数据。

有什么指导吗?我想知道 Schema 是否受到某种保护,也许有一种猫鼬方法可以解决这个问题?

4

1 回答 1

13

将对象与猫鼬断开连接。查询时使用lean()mongoose 的方法获取 JSON 对象。然后,您可以为其添加密钥。

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

于 2018-04-05T01:59:13.450 回答