[英]Getting undefined when updating many using Mongoose
我有一个 Node.js 应用程序,它使用 Mongoose.js 与 MongoDB 交互。 我正在尝试使用它,以便在发生某个操作(更新计划)时,它会使用某个 companyID(即属于该公司的所有用户)更新所有用户。 下面我的代码返回 undefined 找到了多少以及更新了多少。
const updated = User.updateMany({ companyID: req.body.companyID }, { 'company.stripe.plan': req.body.plan });
console.log(updated.n)
console.log(updated.nModified)
请求体
{ company:
{ stripe:
{ plan: '<>',
subscriptionId: '<>',
customerId: '<>',
last4: '<>' },
companyName: '<>'},
isVerified: true,
_id: '<>',
email: '<>',
companyID: 'fc5a653c-2f68-4925-9ff2-93fde2157453',
updatedAt: '2020-02-10T01:32:53.510Z',
createdAt: '2020-02-04T00:44:27.971Z',
__v: 0,
lastLogin: '2020-02-10T00:46:16.118Z',
plan: '<>',
subscriptionId: '<>' }
我编辑了一些不需要且可能不应该共享的信息。 req.body 的第一部分实际上是发送到 API 的单个用户以及底部看到的计划和订阅 ID。
我在第一个代码段中看到的 console.log 返回 undefined 当我希望它返回 2 个找到的记录和 2 个更新/修改的记录时。
编辑** 看到这里不清楚是我缩小的用户模型
var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');
var timestamps = require('mongoose-timestamp');
var bcrypt = require('bcrypt-nodejs');
var UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true
},
password: String,
companyID: {
type: String,
required: true
},
company: {
stripe: {
customerId: String,
subscriptionId: String,
last4: String,
plan: {
type: String,
default: 'default'
},
}
},
lastLogin: Date,
lastChangedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
});
除了需要的功能之外的所有功能都已删除
exports.plan = function(req, res, next) {
...
console.log(req.body)
console.log(req.body.companyID)
const updated = User.updateMany({ companyID: req.body.companyID }, { 'company.stripe.plan': req.body.plan });
console.log(updated.n)
console.log(updated.nModified)
return res.json({success: true})
....
}
已解决,问题是更新的键/值中的键,它不适用于点表示法,但是当我切换到括号表示法时,它现在正在工作。 我认为这可能是 mongoose 的一个限制,因为我觉得我以前遇到过这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.