我有检索 Mongoose 对象的代码,然后使用 stripeCustomerId
(存储在文档中)检索 Stripe customer
对象(通过 nodejs stripe)。然后我想将条纹 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
我正在将 mongoose 对象放回它需要去的地方,但是 stripeCustomer
不是该对象的一部分!我已经验证 stripeCust
实际上正在返回我期望的数据。
有什么指导吗?我想知道 Schema 是否以某种方式受到保护,也许有一种 mongoose 方法可以解决这个问题?
最佳答案
从 mongoose 中断开对象。在进行查询时,使用 mongoose 的 lean()
方法获取 JSON 对象。然后你可以给它添加 key 。
关于javascript - 将键/值对添加到返回的 Mongoose 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49662843/