路径“ visits”上的值“ 1”强制转换为ObjectId

时间:2018-09-07 15:02:27

标签: javascript node.js mongodb mongoose

我不断收到错误消息,在路径“ visits”的值“ 1”投射到ObjectId失败。我将在模式下方显示。

用户架构:

const mongoose = require('mongoose');
const uniqueVal = require('mongoose-unique-validator');

const userSchema = mongoose.Schema({
  email: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  role: {
    type: String,
    required: true
  },
  answers: {
    type: String
  },
  visits: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'UserClicks', // Reference to your Visit model
    }]
});

userSchema.plugin(uniqueVal);

module.exports = mongoose.model('User', userSchema);

然后我具有将它们连接起来的UserClicks架构。

UserClicks架构:

const mongoose = require('mongoose');

const visitCountSchema = mongoose.Schema({
  postId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Post', // Reference to your Post model
  },
  visitCount: {
    type: Number,
    default: 1
  }
})

module.exports = mongoose.model('UserClicks', visitCountSchema);

这是两个模式,然后我得到了失败的功能,我将在下面显示:

exports.pageVisitCount = (req, res, next) => {
  User.findById({
    _id: req.userData.userId
  }, 'visits', function (err) {
    if (err) {
      res.status(401).json({
        message: "Error Occured!"
      })
    } else {
      User.findOneAndUpdate({
        "visits.postId" : mongoose.Types.ObjectId(req.body.postId),
        },
      {
        $inc : { "visits.$.visitCount" : 1 }
      }, (err, doc) => {
        if (err) {
          console.log(err);
        } else {
          if (doc === null ) {
            const postToAdd = {
              postId : mongoose.Types.ObjectId(req.body.postId)
            };
            User.findByIdAndUpdate({
              _id: mongoose.Types.ObjectId(req.userData.userId)
            },
            {$push: { visits : postToAdd }},
            (err, doc) => {
              if(err) {
                res.status(401).json({
                  message: "Error Occured!"
                })
              } else {
                res.status(200).json({
                  message: "Success!"
                })
              }
            }
          );
          } else {
            res.status(200).json({
              message: "Success!"
            })
          }
        }
      });
    }
  });
}

它似乎失败了:

$inc : { "visits.$.visitCount" : 1 } }, (err, doc) => {

我以前曾使用过此功能,但现在我更改了模型并将它们分开放置,但是它不起作用。有任何想法吗?谢谢

这是下面的错误日志:

{ CastError: Cast to ObjectId failed for value "1" at path "visits"
    at new CastError (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/error/cast.js:29:11)
    at ObjectId.cast (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/schema/objectid.js:153:13)
    at ObjectId.SchemaType.applySetters (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/schematype.js:755:12)
    at ObjectId.SchemaType._castForQuery (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/schematype.js:1141:15)
    at ObjectId.SchemaType.castForQuery (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/schematype.js:1131:15)
    at ObjectId.SchemaType.castForQueryWrapper (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/schematype.js:1110:15)
    at castUpdateVal (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/helpers/query/castUpdate.js:383:21)
    at walkUpdatePath (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/helpers/query/castUpdate.js:256:22)
    at castUpdate (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/helpers/query/castUpdate.js:79:18)
    at model.Query._castUpdate (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/query.js:3714:10)
    at castDoc (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/query.js:3741:18)
    at model.Query.Query._findAndModify (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/query.js:2965:19)
    at model.Query.Query._findOneAndUpdate (/Users/andrewpeliza/Desktop/ctas/node_modules/mongoose/lib/query.js:2674:8)
    at process.nextTick (/Users/andrewpeliza/Desktop/ctas/node_modules/kareem/index.js:333:33)
    at process._tickCallback (internal/process/next_tick.js:61:11)
  message: 'Cast to ObjectId failed for value "1" at path "visits"',
  name: 'CastError',
  stringValue: '"1"',
  kind: 'ObjectId',
  value: 1,
  path: 'visits',
  reason: undefined }

0 个答案:

没有答案