我不知道为什么会收到此错误“ TypeError:无法读取未定义的属性'findIndex'”

时间:2019-05-21 23:47:40

标签: node.js mongoose

在您的帮助下,我创建了一个函数,用于在数组中搜索id,如果发现它在做某件事,否则它将被添加到该数组中。

exports.postPositiveRank = async (req,res,next) =>{
    const targetUser = req.body.targetUserId;
    const loggedInUser = req.session.user._id;
       try{
            let user = await User.findOne({"_id": targetUser})
            const index=user.postitiveRanked.findIndex(id=>id===loggedInUser)
            if(index!==-1)
              {
               //userfound
              }
            else{
                user.positiveRanked.push(loggedInUser);
                await user.save()
             }
          }
       catch(error){
            console.log(error)
          }
    }

以及我的用户架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema =  new Schema({
firstname: {
    type: String,
    required: true
},
lastname: {
    type: String,
    required:true
},
age: {
    type: Number,
    required:true
},
occupation: {
    type: String,
    required:true
},
city: {
    type: String,
    required: true
},
county: {
    type: String,
    required:true
},
country: {
    type:String,
    required: true,
},
email: {
    type: String,
    required:true
},
password: {
    type: String,
    required:true
},
imageUrl: {
    type: String,
    required:true
},
rate:{
    type:Number,
    required:true
},
positiveRanked: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
}],
negativeRanked: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
}]

});



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

当我尝试发出请求时,我得到了这个错误

  

“ TypeError:无法读取未定义的属性'findIndex'       在exports.postPositiveRank(/用户/ calinonaca /桌面/ Mirror-Final /控制器/feed.js:60:46)       在processTicksAndRejections(internal / process / task_queues.js:86:5)“

能帮我吗?我提到数组应该为空,我不希望有任何值。

0 个答案:

没有答案