const results = async () => { return(await Moralis.Cloud.run("searchResults", params))}
Do you know why nothing is returning?
const results = async () => { return(await Moralis.Cloud.run("searchResults", params))}
Do you know why nothing is returning?
It looks like this works fine for me in a react app:
import { useMoralis } from "react-moralis";
function App() {
const { Moralis } = useMoralis();
const x = async () => {
try {
const x3 = await Moralis.Cloud.run("test_result");
console.log("x3", x3);
} catch (e) {
console.log(e);
}
};
return (
<div>
<p>
<button onClick={x}> test button</button>
</p>
</div>
);
}
export default App;
I copy and pasted the exact thing but replaced console.log with returnā¦didnāt get anything, not even an error was returnedā¦ Also as you see Iām in a class component so I canāt useMoralis()
This works too:
const Moralis = require("moralis");
function App2() {
const x = async () => {
try {
const x3 = await Moralis.Cloud.run("test_result");
console.log("x3", x3);
} catch (e) {
console.log(e);
}
};
return (
<div>
<p>
<button onClick={x}> test button 2</button>
</p>
</div>
);
}
export default App2;
If you let that console.log and look in the console you should see the output
class Navy extends Component {
constructor(props) {
super(props);
this.state = {
showsearch: false,
searchcontent: null
}
this.openSearch = this.openSearch.bind(this);
this.closeSearch = this.closeSearch.bind(this);
this.handleChange = this.handleChange.bind(this)
}
openSearch() {
this.setState({ showsearch: true });
}
closeSearch() {
let vm = this;
setTimeout(() => {
vm.setState({ showsearch: false });
}, 100);
}
handleChange(e) {
var value = e.target.value;
this.setState({searchcontent: value});
}
render() {
const Moralis = require('moralis')
const params = { value: this.state.searchcontent};
const results = async () => {
try {
const x3 = await Moralis.Cloud.run("searchResults", params);
return("x3", x3);
} catch (e) {
return(e);
}
};
return(
<Modal show={this.state.showsearch} onHide={this.closeSearch} size='lg' centered >
<Modal.Header >
<Modal.Title>Search</Modal.Title>
</Modal.Header>
<Modal.Body>
Search Results #: {results.length} <br/>
Result (JSON): {JSON.stringify(results)} <br/>
Result: {results}
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={this.closeSearch}>
Close
</Button>
</Modal.Footer>
</Modal>
)}}
literally nothing is returningā¦ no error, not even the āx3ā string
I switched my button to onClick={x} and it did console.log the arrayā¦ do you think thereās a reason it wonāt return on the modal? not even results.length
@cryptokid noticed the right thing:
That is probably because you defined result as a function
you could return something in that function maybe
Where is that? is that app id or server url?
You have inserted a function to render, not its result
this is my current code though:
const Moralis = require('moralis')
const params = { value: this.state.searchcontent};
const results = async () => {
try {
const x3 = await Moralis.Cloud.run("searchResults", params);
return(x3);
} catch (e) {
return(e);
}
};
I made it a function because so far Iāve had no other option when using await, Iād get errors
Share your server url
Try this:
import React, { useState } from "react";
const Moralis = require("moralis");
function App2() {
const [results, setResults] = useState("");
Moralis.start({
appId: "APP_ID",
serverUrl: "SERVER_URL"
});
React.useEffect(() => {
const asyncFunc = async () => {
const x3 = await Moralis.Cloud.run("test_result");
setResults(JSON.stringify(x3));
};
asyncFunc();
}, []);
return (
<div>
<p>test {results}</p>
</div>
);
}
export default App2;
Try this:
const Moralis = require("moralis");
class Navy extends Component {
constructor(props) {
super(props);
this.state = {
showsearch: false,
searchcontent: null,
searchResults: "",
};
this.openSearch = this.openSearch.bind(this);
this.closeSearch = this.closeSearch.bind(this);
this.handleChange = this.handleChange.bind(this);
}
openSearch() {
this.setState({ showsearch: true });
}
closeSearch() {
let vm = this;
setTimeout(() => {
vm.setState({ showsearch: false });
}, 100);
}
handleChange(e) {
var value = e.target.value;
this.setState({ searchcontent: value });
}
componentDidMount() {
const params = { search: this.state.searchcontent };
Moralis.Cloud.run("userSearch", params).then((result) =>
this.setState({ searchResults: result })
);
}
render() {
return (
<Modal show={this.state.showsearch} onHide={this.closeSearch} size="lg" centered>
<Modal.Header>
<Modal.Title>Search</Modal.Title>
</Modal.Header>
<Modal.Body>
Search Results #: {this.state.searchResults.length} <br />
Result (JSON): {JSON.stringify(this.state.searchResults)} <br />
Result: {this.state.searchResults}
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={this.closeSearch}>
Close
</Button>
</Modal.Footer>
</Modal>
);
}
}
Unfortunately didnt work
ethershare should be returning 1 object though