Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Object { _id: 5219f3858ec1ba1c28000001 } has no method 'attach' #31

Closed
henrynok opened this issue Aug 25, 2013 · 10 comments
Closed
Labels

Comments

@henrynok
Copy link

Hi I am new to mongoose-attachments. When I try the example I get a TypeError: Object { _id: 5219f3858ec1ba1c28000001 } has no method 'attach' .

I have tried everything please advise if I am doing anything wrong. I use the latest versions of Mongoosejs, Node and Express.

I tried to use it with all providers but no luck.

@nuarhu
Copy link
Contributor

nuarhu commented Aug 26, 2013

Could you please post the code where you are initalizing and attaching mongoose-attachments?

Please make sure that you use the documentation of the stable version https://github.com/heapsource/mongoose-attachments/tree/v0.0.4 .

@vinh0604
Copy link

Hi, I face the same problem today. Here is my model config:

AttachmentSchema.plugin(attachments, {
  directory: '/app/uploads/startups/logos',
  storage : {
    providerName: 'localfs'
  },
  properties: {
    image: {
      styles: {
        original: {
          // keep the original file
        },
        thumb: {
          thumbnail: '100x100^',
          gravity: 'center',
          extent: '100x100',
          '$format': 'jpg'
        },
        detail: {
          resize: '400x400>',
          '$format': 'jpg'
        }
      }
    }
  }
});

module.exports = mongoose.model('Attachment', AttachmentSchema);

And in controller:

    var attachment = new Attachment();

    attachment.attach('image', req.files.file, function (err) {
      if (err) {
        return next(err);
      }

      attachment.save(function (err, attachment) {
        if (err) {
          res.status = 422;
          res.json({success: false});
        }

        res.json({success: true, attachment: attachment});
      });
    });

@vinh0604
Copy link

After debugging, I realized that the mongoose-attachments-localfs does not return the referrence for mongoose-attachments as mentioned in the doc.

I solved the problem by directly require the mongoose-attachments and call the plugin() method via it.

@henrynok
Copy link
Author

Hi vinh0604,

Could you please paste the code for your solution?

@nuarhu
Copy link
Contributor

nuarhu commented Aug 30, 2013

Thanks for your comment. It made me realize that the tag 0.1.0 is too old. The master version (untagged) of mongoose-attachments-localfs correctly exports a reference to attachments. Sorry for that. The documentation was ahead of the code for some time.

@henrynok
Copy link
Author

Will you be tagging a new version to correct the problem or should we use the master version?

@nuarhu
Copy link
Contributor

nuarhu commented Aug 30, 2013

As a workaround:

require('mongoose-attachments-localfs');
var attachments = require('mongoose-attachments');

should work.

@nuarhu
Copy link
Contributor

nuarhu commented Aug 30, 2013

@henrynok
The next release is actually planned. It is done by @thepumpkin1979 .
Until then either use master or the workaround + tag 0.1.0.

@nuarhu
Copy link
Contributor

nuarhu commented Aug 30, 2013

Fixed in current master of mongoose-attachments-localfs (not yet in tag 0.1.0).

@nuarhu nuarhu closed this as completed Aug 30, 2013
@rngadam
Copy link

rngadam commented Oct 11, 2013

I think it would be really important to release this fix as I just spent 2 hours tracking down this problem. Sure, part of it was because I'm new to MongooseJS and wasn't sure if I was doing something wrong but still...

Or perhaps at least a README note?

The workaround wasn't working for me, but changing my package.json to:

"mongoose-attachments-localfs": "git+ssh://git@github.com:heapsource/mongoose-attachments-localfs.git#master",

...did fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants