我需要帮助,因为我现在对Promises和Cloud Code的使用还很陌生。我在stackoverflow上查看了其他问题,但它们似乎都是使用过时的Parse.Promise类。
注意:我正在本地安装的parse-server上工作,以便能够更容易地测试我的Cloud Code。
我有2个函数。
Parse.Cloud.define("getTag", async (request) => {}
Parse.Cloud.define("createTag", async (request) => {}
基本上,我调用getTag传递一个字符串。如果不存在,它会通过调用createTag函数创建标签,然后返回新的Tag对象。
无论哪种方式,我都应该得到一个 Tag 对象。
我的 getTag 函数工作得很好,并返回了现有的对象。但我卡住了,无法从createTag函数中得到新创建的Tag对象,无法通过返回到getTag函数。
即使Tag被正确创建,并且在我检查数据库时如预期的那样最终出现在Tag类中,我还是得到了这个错误。
ParseError: The server returned an invalid response.
at <path>/node_modules/parse-server/node_modules/parse/lib/node/Cloud.js:163:13
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 107
}
基本上我不确定是否正确地返回了createTag的对象
这是我的2个函数。希望得到任何帮助。
Parse.Cloud.define("getTag", async (request) => {
console.log("===== Begin getTag ====== \n\n\n")
const Tag = Parse.Object.extend("Tag");
const query = new Parse.Query(Tag);
query.equalTo("tag", request.params.tag);
await query.first().then(
function(foundTag) {
// Check if we got an object back.
if (foundTag === undefined || foundTag === null) {
// We have to create a new one.
console.log("NO TAG FOUND:\nLet's create a new tag with - ", request.params.tag)
Parse.Cloud.run("createTag", {tag: request.params.tag}).then(function(createdTag) {
// PROBLEM IS HERE - I CAN'T get createdTag object.
return createdTag;
})
.catch(function(error) {
// There was an error.
console.log(error)
return error
});
} else {
// We found an existing Tag Object
console.log("Found an EXISTING TAG:", foundTag);
console.log("\n\n\n")
return foundTag;
}
}).catch(function(error) {
// There was an error.
console.log(error)
return error
});
});
Parse.Cloud.define("createTag", async (request) => {
console.log("n\n ===== Begin createTag ====== \n\n\n")
var tagString = request.params.tag
const TagClass = Parse.Object.extend("Tag");
const Tag = new TagClass();
Tag.set("tag", tagString);
const results = await Tag.save().then(function(newTag) {
// Execute any logic that should take place after the object is saved.
console.log("TAG CREATED:", newTag);
console.log("\n\n\n")
/*
THIS WORKS FINE. newTag object prints out
TAG CREATED: ParseObjectSubclass {
className: 'Tag',
_objCount: 3,
id: 'DOaiQGuzLB'
}
*/
return newTag;
})
.catch(function(error) {
// There was an error.
console.log(error)
return error
});
});
这是Cloud.js 163中的一行。
throw new _ParseError.default(_ParseError.default.INVALID_JSON, 'The server returned an invalid response.');
我终于想明白了,这是个悲哀的问题。
这一切都归结于我没有理解.then和async函数的工作结构。我以为.then()里面的return是指整个函数的return。当我发现不是这样的时候,用链子把返回函数链起来就解决了。
我的Parsse.cloud.run("createTag")函数本身没有返回对象,所以直到getTag函数都在返回undefined。
解决的办法只是在createTag函数的最后加上 "return results"。
Parse.Cloud.define("createTag", async (request) => {
console.log("n\n ===== Begin createTag ====== \n\n\n")
var tagString = request.params.tag
const TagClass = Parse.Object.extend("Tag");
const Tag = new TagClass();
Tag.set("tag", tagString);
const results = await Tag.save().then(function(newTag) {
// Execute any logic that should take place after the object is saved.
console.log("TAG CREATED:", newTag);
console.log("\n\n\n")
return newTag;
})
.catch(function(error) {
// There was an error.
console.log(error)
return error
});
// ADDED THIS
return results
});
至少现在我对async和promises的工作原理有了更好的理解。我希望这能为其他像我这样的新手开发者节省一些时间。