简体   繁体   English

使用 Mongoose 更新许多时未定义

[英]Getting undefined when updating many using Mongoose

I have a Node.js application which uses Mongoose.js to interface with MongoDB.我有一个 Node.js 应用程序,它使用 Mongoose.js 与 MongoDB 交互。 I am trying to have it so that when a certain action happens (updating a plan) that it then updates all user's with a certain companyID (ie all users who belong to that company).我正在尝试使用它,以便在发生某个操作(更新计划)时,它会使用某个 companyID(即属于该公司的所有用户)更新所有用户。 Below my code returns undefined for how many found as well as how many updated.下面我的代码返回 undefined 找到了多少以及更新了多少。

const updated = User.updateMany({ companyID: req.body.companyID }, { 'company.stripe.plan': req.body.plan });
console.log(updated.n)
console.log(updated.nModified)

req.body请求体

{ 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: '<>' }

I've redacted some info that isn't needed and probably shouldn't be shared.我编辑了一些不需要且可能不应该共享的信息。 The first part of the req.body is actually a single user being sent to the API along with the plan and subscriptionId seen at the bottom. req.body 的第一部分实际上是发送到 API 的单个用户以及底部看到的计划和订阅 ID。

My console.log seen in my first snippet are returning undefined when I would expect it to return 2 records found and 2 updated/modified.我在第一个代码段中看到的 console.log 返回 undefined 当我希望它返回 2 个找到的记录和 2 个更新/修改的记录时。

EDIT** Seeing as it wasn't clear here is my minified User model编辑** 看到这里不清楚是我缩小的用户模型

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

Function with all but the needed removed除了需要的功能之外的所有功能都已删除

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

Resolved, issue was the key in the key/value for the update, it was not working with dot notation however when I switched to bracket notation is is now working.已解决,问题是更新的键/值中的键,它不适用于点表示法,但是当我切换到括号表示法时,它现在正在工作。 I think this might be a limitation of mongoose as I have a feeling I ran into this error before.我认为这可能是 mongoose 的一个限制,因为我觉得我以前遇到过这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM