简体   繁体   中英

Getting undefined when updating many using Mongoose

I have a Node.js application which uses Mongoose.js to interface with 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). Below my code returns undefined for how many found as well as how many updated.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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