Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查用户在阵列中已经有4个图像,以及don';不让我们继续Node.js Express中的下一步吗?_Node.js_Mongodb_Express_Mongoose_Mongoose Schema - Fatal编程技术网

如何检查用户在阵列中已经有4个图像,以及don';不让我们继续Node.js Express中的下一步吗?

如何检查用户在阵列中已经有4个图像,以及don';不让我们继续Node.js Express中的下一步吗?,node.js,mongodb,express,mongoose,mongoose-schema,Node.js,Mongodb,Express,Mongoose,Mongoose Schema,我正在尝试设置图像的计数限制。问题是,我已经在Mongoose中设置了它,但对于上传到云端的图像,我认为应该在上传之前添加一些内容,并首先检查图像数组的长度是否小于4。这是代码 猫鼬 const userSchema = new mongoose.Schema({ username: { type: String }, email: { type: String }, isVerified: { type: Boolean, default: false }, picVersio

我正在尝试设置图像的计数限制。问题是,我已经在Mongoose中设置了它,但对于上传到云端的图像,我认为应该在上传之前添加一些内容,并首先检查图像数组的长度是否小于4。这是代码

猫鼬

const userSchema = new mongoose.Schema({
  username: { type: String },
  email: { type: String },
  isVerified: { type: Boolean, default: false },
  picVersion: { type: String, default: '1531305955' },
  picId: { type: String, default: 'default.png' },
  images: {
    type:[{
      imgId: { type: String, default: '' },
      imgVersion: { type: String, default: '' }
    }],
    validate: [arrayLimit, 'You can upload only 4 images']
  },
  city: { type: String, default: '' },
});


function arrayLimit(val) {
  return val.length <= 4;
}
constuserschema=newmongoose.Schema({
用户名:{type:String},
电子邮件:{type:String},
isVerified:{type:Boolean,默认值:false},
picVersion:{type:String,默认值:'1531305955'},
picId:{type:String,默认值:'default.png'},
图像:{
类型:[{
imgId:{type:String,默认值:''},
imgVersion:{type:String,默认值:“”
}],
验证:[arrayLimit,'您只能上载4个图像']
},
城市:{类型:字符串,默认值:''},
});
函数arrayLimit(val){
返回值长度
物件
.status(HttpStatus.OK)
.json({消息:“图像已成功上载”})
)
.catch(错误=>
物件
.status(HttpStatus.INTERNAL_SERVER_错误)
.json({消息:“上载图像时出错”})
);
});
},
cloudinary.uploader.upload(req.body.image,async result=>{

首先检查一下吗?

实际上,你可以改变你更新伴侣的方式。。 我建议您先使用findOne()然后再使用update-your-User,因为这样做时,您对更新的控制比使用update更大。因此,请尝试这样做

UploadImage(req, res) {
User.findOne({ _id: req.user._id })
.then((user) => {
    if (user) {
        if (user.images.type.length <= 4) {
            cloudinary.uploader.upload(req.body.image, async result => {
                user.image.type.push({ imgId: result.public_id, imgVersion: result.version });
                user.save()
                    .then(res.status(HttpStatus.OK).json({ message: 'Image uploaded successfully' }))
                    .catch(); //use callback inside save() or use it like promise
            });

        }
    } else { res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Error uploading image' }); }
})
.catch(err => res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Error uploading image' }));
UploadImage(请求、恢复){
findOne({u id:req.User.\u id})
。然后((用户)=>{
如果(用户){
if(user.images.type.length{
user.image.type.push({imgId:result.public_id,imgVersion:result.version});
user.save()
.then(res.status(HttpStatus.OK).json({message:'Image upload successfully'}))
.catch();//在save()内使用回调,或像promise一样使用它
});
}
}else{res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({message:'ERROR upload image'});}
})
.catch(err=>res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({message:'ERROR upload image'}));
}


通过这样做,你可以实现你想要的……干杯:)

谢谢你的回答。但关键是我需要在Cloudinary.uploader之前检查它,而不是之后。因为如果我用这种方式,云中会挤满大量的图像。哦,好吧,在这种情况下,你可以像我一样做,但只有当请求到达端点时,你才能查询用户并按照我的方式做。我已经编辑了,请再次检查。现在你会找到你想要的方式。我会很快测试它并告诉你。再次感谢。没问题,如果你觉得这很有帮助并且有效,请确保你标记了答案,伙计:)
UploadImage(req, res) {
User.findOne({ _id: req.user._id })
.then((user) => {
    if (user) {
        if (user.images.type.length <= 4) {
            cloudinary.uploader.upload(req.body.image, async result => {
                user.image.type.push({ imgId: result.public_id, imgVersion: result.version });
                user.save()
                    .then(res.status(HttpStatus.OK).json({ message: 'Image uploaded successfully' }))
                    .catch(); //use callback inside save() or use it like promise
            });

        }
    } else { res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Error uploading image' }); }
})
.catch(err => res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Error uploading image' }));