我創建了一個功能來查找對象:爲什麼下面的_.map函數什麼都不返回?
store.js
store.find =() => {
const query = new Parse.Query(Document)
return query.find()
}
,我使用這樣的(和分配價值this.documents
):
main.js
store.find().then((results) => {
this.documents = _.map(results, (result) => {
return result.toJSON()
})
})
它的工作原理。但store.find().then(...)
部分在main.js
重複很多次,所以我把它變成一個功能:
store.js
store.fetch = (documents) => {
return store.find().then((results) => {
documents = _.map(results, (result) => {
return result.toJSON()
})
})
}
,我用這樣的:
main.js
store.fetch(this.documents)
但是沒有東西被分配給this.documents
並且沒有錯誤消息。可能是什麼問題?
注意:this.documents
是一個對象數組。例如:
[{
title: this.title,
content: this.content,
}, ...
編輯:
我這樣做:
store.fetch = (documents) => {
store.find().then((results) => {
documents = _.map(results, (result) => {
return result.toJSON()
})
console.log(documents)
})
}
而且documents
被分配:
[Object, Object, Object, ...]
所以我認爲它只是不分配這個數組到this.documents
。也許變量不能是參數,並且可以同時賦值?
嘛,你不* *返回任何東西,你只是分配給'documents'。 – deceze