0

I am creating a new Product, after saving it I proceed to search for it with mongoose findById, then I try yo create another document "Image" which is referenced inside the Product schema. When I execute the code, it gives me TypeError: image.save is not a function. How should I fix this?

Product.create(req.body.product, function(err, product) {
        if (err) {
            console.log(err);
        } else {
            product.save();
            Product.findById(product._id, function(err, foundProduct) {
                if (err) {
                    req.flash('error', err.message);
                    res.redirect('/product');
                } else {
                    Image.create(req.files, function(err, image) {
                        if (err) {
                            req.flash('error', err.message);
                            res.redirect('/product');
                        } else {
                            image.save();
                            foundProduct.images.push(image);
                            foundProduct.save();
                            res.redirect('/product/' + product._id);
                        }
                    });
                }
            });
        }
    });

This are my schemas:

var productSchema = new mongoose.Schema({
    name: String,
    price: String,
    description: String,
    gender: String,
    images: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Image"
    }],
    sizes: {
        ch: Number,
        m: Number,
        g: Number,
        eg: Number
    },
    type: String,
    likes: Number, default: 0
});


var imageSchema = new mongoose.Schema({
    public_id: String,
    url: String,
    secure_url: String,
    resource_type: String,
    format: String,
    bytes: String
});
tFranzoni
  • 17
  • 2

1 Answers1

0

Shouldn't you declare your image as a new object of the Schema ImageSchema? It seems to me that you did not create the object.

Inside your function I would do the following:

const im = new Image();
im.url = ...

Then I would make a call for the save function.

im.save().then((data,error) => {...})
Israel Zinc
  • 2,713
  • 2
  • 18
  • 30
  • Thanks that helped me a lot! Any suggestions on why the .create method did't work in this case? I have other parts of the code where it works like a charm. – tFranzoni Feb 13 '17 at 23:20
  • To be honest with you, I've never used this .create function. The .save function was always my main way of saving documents to db. But, I looked it up online and it seems like this create is a more generic functions to save objects (that not necessarily needs to be an instance of the model). While in .save the object need to be instantiated (like I did with the "new Image()"). I never had any problems with the save, so I believe you would not have a problem if you only use it. http://stackoverflow.com/questions/38290684/mongoose-save-vs-insert-vs-create http://mongoosejs.com/docs/api.html – Israel Zinc Feb 14 '17 at 01:36