-
Notifications
You must be signed in to change notification settings - Fork 76
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
Cannot get attachments working (error in serializer) #229
Comments
Could we see your model file? At first glance it looks like you have |
@jlami the model is available in the gist (file called |
I think there might be a bug in the The following should work with null for the empty state:
I will see if I can make a PR for this. I have only tested this with a 'local' pouchDB, not a couch database. I think your deserialize problem stems from wrong initial data in couch. The 'avatar' value should not be set as far as I know. It should only be on the _attachments. But I will see if I can test this with a couch db to verify. ---edit This is the document I get when I use a couchdb:
BTW when using the transform with the s, you have to do --edit2 |
Ok so I created a new // /app/transforms/attachment.js
import { isNone } from '@ember/utils';
import { AttachmentsTransform } from 'ember-pouch';
export default AttachmentsTransform.extend({
deserialize: function(serialized) {
let atts = this._super(serialized);
console.log('deserialize', atts);
if (atts.length > 0)
return atts.pop();
else
return null;
},
serialize: function(deserialized) {
if (isNone(deserialized)) { return {}; }
return this._super([deserialized]);
}
}); In CouchDB, my doc looks like that: {
"_id": "user_2_XXX",
"_rev": "1-XXX",
"data": {
"email": "user2@myapp.com",
"username": "user2",
"avatar": null
}
} In my controller, I do this: // /app/controllers/user.js
actions: {
test() {
let avatarDOMElement = document.getElementById('user-avatar-input');
if (avatarDOMElement.files.length > 0) {
let file = avatarDOMElement.files[0];
this.set('model.avatar', file);
this.get('model').save();
}
}
} But at runtime, I still get the same errors as before, down to the serializer. Am I doing it wrong? Edit: // /app/models/user.js
avatar: DS.attr('attachment', {
defaultValue: function() {
return null;
}
}), |
I haven't exactly looked at what couch will store if you set it to null. I'll try that later today. But maybe it is an idea to see how a new model is saved if you do set a file? So don't load anything in the route // /app/controllers/user.js
//import {inject} from '@ember/service';
store: inject(),
actions: {
test() {
let avatarDOMElement = document.getElementById('user-avatar-input');
if (avatarDOMElement.files.length > 0) {
let file = avatarDOMElement.files[0];
let model = this.store.createRecord('user', {username: 'user2', email: 'user2@myapp.com'});
model.set('avatar', file);//also try without this line to see null result
model.save();
}
}
} That way you would not run into deserialize problems of the existing data. |
Thank's to your idea @jlami, I spotted an error in my custom Inside the new Edit And the payload of the PUT request is this: Obviously, the request fails with a "Bad request: Invalid attachment data for undefined". |
I think you might need to import the I didn't know deserialize got the default value, might be a good idea to use that yes. But I would expect ember-data to set that internally if we return something empty? I would have to look into that. |
Argh, sorry I messed it! Thanks. So, I tried without setting the attachment and I get model.set('avatar', Ember.Object.create({
name: file.name,
content_type: file.type,
data: file
})); Now the document inside CouchDB is correct and I'm able to retrieve stub data of that attachment: {
"_id": "user_2_XXX",
"_rev": "1-XXX",
"data": {
"email": "user@myapp.com",
"username": "user",
"avatar": {
"ember.png": {}
}
},
"_attachments": {
"ember.png": {
"content_type": "image/png",
"revpos": 1,
"digest": "md5-uIVjw2LSzOiPMhbakNbwCQ==",
"length": 103382,
"stub": true
}
}
} Now I have to figure out how to get the non-stubbed attachment's data, but it seems others tried it some time ago 🤔. Anyway, thank you very much @jlami, you saved me (and my customer OFC 😂)! Maybe we should update the README according to what we found? Edit: I tried something like that and it work as expected 👍. model.get('documents').addObject(EmberObject.create({
name: file.name,
content_type: file.type,
data: file
})); |
Good to hear that you got it working. But I think you are right that getting the actual file back needs some more work. Don't know if there is a PR for that yet, but you can indeed look at the issue you mentioned to get the code you need in your own adapter for now. Let's see what will have to be changed in ember-pouch to make this easier, and if you have any suggestions for the README, those are more than welcome 😄 |
Hi there!
I'm having a hard time working with
ember-pouch
(v6.0.0) and attachments on aember-cli
v3.3 app.According to the README, I've setup my model like this (stripped down) gist.
The user selects a file with the
<input type="file">
, I get theFile
object in the controller and then add it to theUser
model as the README says (here as a Blob, but I also tried the base-64 way).That throws me an error in the Serializer: Uncaught TypeError: Cannot convert undefined or null to object.
When the error throws,
fileNames
isundefined
,attributes
looks like this:In CouchDB, the user's data look like that:
I've added the
avatar
field mysel, but it don't work without it anyway.Everything looks ok, so I don't get why it breaks there. And I was unable to find any kind of help about that so here I am...
Let me know if you need more info!
The text was updated successfully, but these errors were encountered: