We'll continue building out the profile updating feature of our application by defining the update route in our Sails app. In this route, we'll take care of updating our user's information as well as uploading a user avatar image when present. I'll go ahead and copy paste the upload code in this lesson, we'll look at reducing code redundancy in the next lesson.

Comments (8)
AlfieLBTA
3 years ago
Hey Brian: i get a 500 error while trying to upload (error: Sending 500 ("Server Error") response: Error: Request aborted) But the funny thing is that i can create posts just fine and the upload methods are identical any idea what else can i check?
Setwork44
3 years ago
dude all my users keep deleting off my database!!! O,O how is this happening????
Setwork44
3 years ago
my avatar pics arent being persisted? can anyone help?
Setwork44
3 years ago
can anyone figure out why this code isn't uploading the avatar?? module.exports = async function(req,res) { console.log("Trying to update user...") const fullName = req.body.fullName const bio = req.body.bio const jobTitle = req.body.jobTitle const website = req.body.website const file = req.file('imagefile') console.log(file) //no avatar file change was found if (file.isNoop) { await User.update({id: req.session.userId}) .set({fullName: fullName, bio: bio, jobTitle: jobTitle, website: website }) file.upload({noop: true}) return res.end() } //ill handle the avatar later const options = { adapter: require('skipper-better-s3'), key: ----', secret: '----', bucket: 'setwork-images', s3params: { ACL: 'public-read' } } file.upload(options, async (err, files) => { if (err) { return res.serverError(err.toString()) } const fileUrl = files[0].extra.Location const userId = req.session.userId const record = await User.update({id: userId}) .set({fullName: fullName, bio: bio, profileImageUrl: fileUrl}).fetch() console.log(JSON.parse(JSON.stringify(record))) res.end() }) }
Setwork44
3 years ago
ahhh shoot I just exposed my secret key.. can someone please help???
Brian Voong
3 years ago
Setwork44
3 years ago
Thank you, I had to create a new key n bucket in aws. My main post pics are uploading but the avatar one still isn't. ill keep trying but honestly I just feel like crying in a corner...
Brian Voong
3 years ago
Setwork44
3 years ago
Maybe its because im trying to display job title and website on the profile page also, that could be messing it up? But I linked it all properly in routes.js... Basically, everything went wrong after this very moment: 04:00 in this video.. job title and website was updating on my user, then the moment I copied your code re avatar it all went wrong. I get this warning sometimes "[DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated" maybe this has something to do with it?
Setwork44
3 years ago
I deleted the whole update.js file and started again. The only improvement I found was Location.reload() I changed to Location.reload(true) and that actually worked. But in regards to uploading the avatar it’s still not saving to aws s3... I’ve literally followed every step to a Tee.. I don’t know what to do?
gerkov77
2 years ago
when adding: await User,update({id: req, session.userId}). set({fullName: fullName, bio: bio}) i get server error with code 500 : TypeError: User.update is not a function
gerkov77
2 years ago
Any Idea fixing this? Did anybody experience the same? I have no idea how to fix this.
gerkov77
2 years ago
Ok, it looks that Visual Studio Code has added an unnecessary require import for User : const User = require("../../models/User"), Deleting this, the error gone. I think I faced something similar earlier in the summer, when I started this tutorial - but it was long time ago for me to still remember this kind of errors.
gerkov77
2 years ago
When I am logging out 'fileURL' for the new avatar, I get a valid url pointing to the aws bucket , where i can see the newly uploaded picture! But when logging out the 'record' constant, I have an empty string for profileImageUrl. This is why I am not seeing the change! But why do I have an empty profileImageUrl on on the record, after I am assigning fileURL to it ? (yes, I commented out startup.png on profile.ejs as on the video) ???
gerkov77
2 years ago
Okay, finally! This is the big difference between working with Xcode, and working with JavaScript in VSCode.. I missed an 'e' from profileImageUrl (wrote -> profilImageUrl,) and nothing complained in the editor of course. I have added the missing 'e', and finally got everything working again! Pheeewww! Got me sweating there!
Brian Voong
2 years ago
gerkov77
2 years ago
Hey Brian, I followed every step in the tutorial, but when I am trying to change the avatar pic, the program doesn't seem to pass the if check-- if the file is noop. isNoop is always true, and the startup icon changes back on save. I what can be the reason. Here's my code: module.exports = async function(req, res) { console.log("trying to update user") const fullName = req.body.fullName const bio = req.body.bio const file = req.file('imagefile') console.log(file) if (file.isNoop) { await User.update({id: req.session.userId}) .set({fullName: fullName, bio: bio}) file.upload({noop: true}) return res.end() } // avatar upload console.log("not NOOP") const options = { adapter: require('skipper-better-s3') , key: 'XXXXXXXXXXXXXXXX' , secret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' , bucket: 'fullstack-social-XXXX' , s3params: { ACL: 'public-read' } // And while we are at it, let's monitor the progress of this upload , onProgress: progress => sails.log.verbose('Upload progress:', progress) } file.upload(options, async (err, files) => { if (err) { return res.serverError(err.toString())} // ... Continue as usual console.log(files) const fileURL = files[0].extra.Location const userId = req.session.userId const record = await User.update({id: userId}).set({fullName: fullName, bio: bio, profilImageUrl: fileURL}) .fetch() console.log(record) res.end() }) }
gerkov77
2 years ago
Ok, the problem was formData.append('avatarFile', this.user.avatarFile) instead of formData.append('avatarFile', this.avatarFile) in profile.ejs. Still does not work though. Console is explaining about files[0].extra.Location, telling me, that property 'extra' is undefined! Any Ideas?
gerkov77
2 years ago
This syntax comes with skipper-better-s3 which we were using in the create.js – I have no idea why the program has problem with it this time..
gerkov77
2 years ago
Ok so the last error is gone by fixing 'imagefile' instead of 'avatarfile' in profile.ejs. BUT, the avatar STILL does not update. No errors though.... Maybe even some errors were better – at least I had some hints.
HELP & SUPPORT