Parse platform 在node.js中保存文件
我正试图做一个简单的用户档案图片上传,但无法找到它 我使用的是Node、Express和forgible。以下是我乘坐快车的路线:Parse platform 在node.js中保存文件,parse-platform,Parse Platform,我正试图做一个简单的用户档案图片上传,但无法找到它 我使用的是Node、Express和forgible。以下是我乘坐快车的路线: // Change Profile Picture exports.changeProfilePicture = function(req, res) { var user = new Parse.User.current(); var form = new formidable.IncomingForm(); form.on('f
// Change Profile Picture
exports.changeProfilePicture = function(req, res) {
var user = new Parse.User.current();
var form = new formidable.IncomingForm();
form.on('file', function(name, file) {
console.log("name = " + name);
console.log("file = " + file);
var parseFile = new Parse.File(name, {base64: file.toString('base64', 0, file.length)}); // Convert from buffer object to array
parseFile.save().then(function() {
// The file has been saved to Parse. file's URL is only available
//after you save the file or after you get the file from a Parse.Object.
//Get the function url() on the Parse.File object.
var url = parseFile.url();
user.set('profilePictureMedium', url);
user.save().then(function() {
response.success("Successfully added add profile picture medium.");
}, function(error) {
response.error("Could not add profile picture medium.");
});
},
function(error) {
// The file either could not be read, or could not be saved to Parse.
alert("Error: " + error.code + " " + error.message);
});
});
}
我正在关注Parse论坛和Alberto的答案,这让我用var parseFile=new Parse.file(名称,{base64:file.toString('base64',0,file.length)}行转换文件数据代码>
文件保存得很好,但是当我在用户
对象上设置它,然后尝试保存时,我得到以下错误:
Error: 111 invalid type for key profilePictureMedium, expected file, but got string
15:56:09 web.1 | self._previousSave = self._source.then(function(base64, type) {
15:56:09 web.1 | ^
15:56:09 web.1 | TypeError: Cannot call method 'then' of undefined
显然,我根据阿尔贝托的回答将文件设置为字符串
但是,当我之前有此功能并且没有转换文件时:
var parseFile = new Parse.File(name, file);
我会得到这个错误:
Error: 111 invalid type for key profilePictureMedium, expected file, but got string
15:56:09 web.1 | self._previousSave = self._source.then(function(base64, type) {
15:56:09 web.1 | ^
15:56:09 web.1 | TypeError: Cannot call method 'then' of undefined
更新:我现在可以将文件保存给用户。但是,保存的文件已损坏。以下是相关代码:
var parseFile = new Parse.File(filename, {base64: file.toString('base64', 0, file.length)});
console.log(file);
console.log(parseFile);
console.log(文件):
console.log(解析文件):
您的列“profilePictureMedium”的类型可能是File
,但您只提供了字符串URL
相反,根据Fosco的评论,您应该将该列设置为刚刚上传的文件:
user.set('profilePictureMedium', parseFile);
如果列的类型已经是file,请设置file对象而不是url。。user.set('profilePictureMedium',parseFile)@所以我现在可以设置文件并将其保存到用户。但是,文件链接现在已断开。。。顺便说一句,谢谢你过去一周的帮助。