I created a function to find objects:
store.js
store.find = () => {
const query = new Parse.Query(Document)
return query.find()
}
That I'm using like this (and assigning the value to this.documents
):
main.js
store.find().then((results) => {
this.documents = _.map(results, (result) => {
return result.toJSON()
})
})
It works. But the store.find().then( ...)
part is repeated many times in main.js
so I turned it into a function:
store.js
store.fetch = (documents) => {
return store.find().then((results) => {
documents = _.map(results, (result) => {
return result.toJSON()
})
})
}
Which I use like this:
main.js
store.fetch(this.documents)
But nothing is being assigned to this.documents
and there are no error messages. What's could be the problem?
NOTE: this.documents
is an array of objects. Example:
[{
title: this.title,
content: this.content,
}, ...
EDIT:
I did this:
store.fetch = (documents) => {
store.find().then((results) => {
documents = _.map(results, (result) => {
return result.toJSON()
})
console.log(documents)
})
}
And documents
is being assigned:
[Object, Object, Object, ...]
So I think it's just not assigning this array to this.documents
. Maybe variables can't be arguments and be assigned with a value at the same time?
You are passing this.documents
as an argument and attempting to modify that property inside the function. However, that can't work, because Javascript does not have "pass by reference" . Only "pass by value".
Instead, you can try passing the object and the property name separately:
store.fetch = (obj, prop) => {
return store.find().then((results) => {
obj[prop] = _.map(results, (result) => {
return result.toJSON()
})
})
}
store.fetch(this, 'documents')
Try to get your results in a .then
block after store.fetch(this.documents)
. And don't forget to store context
var self = this;
store.fetch(self.documents).then(function () {
console.log(self.documents);
});
Or usign es6
store.fetch(this.documents).then(() => {
console.log(this.documents);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.