我试图基于用户ID getList()获取显示名称getUserinfo() 我尝试过
问题:为什么我的getUserinfo()无法为getList()返回一个要使用的值?
private getUserinfo(userid: number) {
var result;
let url;
url = `/_api/web/GetUserById(${userid})`;
const opt: ISPHttpClientOptions = {
headers: { "Content-Type": "application/json;odata=verbose" }
};
this.props.spHttpClient
.get(
this.props.context.pageContext.web.absoluteUrl + url,
SPHttpClient.configurations.v1,
opt
)
.then((response: SPHttpClientResponse) => {
response.json().then((json: any) => {
if (json.Title) {
let name = json.Title;
let email = json.Email;
let issiteadmin = json.IsSiteAdmin;
//debugger;
return name; // this has value but it returns nothing in another function I called
}
});
});
}
private getList() {
this.state.data.length = 0;
const qurl =
"/_api/web/lists/getbytitle('list')/items?$select=*&$orderby=Modified desc";
const opt: ISPHttpClientOptions = {
headers: { "Content-Type": "application/json;odata=verbose" }
};
this.props.spHttpClient
.get(
this.props.context.pageContext.web.absoluteUrl + qurl,
SPHttpClient.configurations.v1,
opt
)
.then((response: SPHttpClientResponse) => {
response.json().then((json: any) => {
for (let i = 0; i < json.value.length; i++) {
let authorid = json.value[i].AuthorId;
let editorid = json.value[i].Editorid;
let Authorname = this.getUserinfo(authorid);
let Editorname = this.getUserinfo(editorid);
debugger;
this.setState({
data: [
...this.state.data,
{
Authorname,
Editorname
}
]
});
}
});
});
}
答案 0 :(得分:3)
由于您尚未从getUserInfo
返回任何信息,因此您刚刚调用this.props.spHttpClient.get()
而未返回其值,例如:
private getUserinfo(userid: number) {
...
return this.props.spHttpClient.get( ... )
.then((response: SPHttpClientResponse) => {
return response.json().then((json: any) => {
if (json.Title) {
let name = json.Title;
let email = json.Email;
let issiteadmin = json.IsSiteAdmin;
return name; // this has value but it returns nothing in another function I called
}
});
});
}
这样,当您致电this.getUserinfo(authorid)
时,您将获得一个承诺,您可以按以下方式使用其值:
this.getUserinfo(authorid).then( name => {
// use its name
});
答案 1 :(得分:3)
这是您使用async / await编写它的方式,从而提高了可读性
for (Mat mat : mats) {
Mat newMat = mat.clone();
opencv_imgproc.Canny(mat, newMat, 50.0, 100.0);
opencv_imgproc.blur(newMat, newMat, new Size(2, 2));
opencv_core.MatVector contours = new opencv_core.MatVector();
opencv_core.Mat hierarchy = new opencv_core.Mat();
//Mat hierarchy = new Mat(new double[]{255, 255, 255, 0});
opencv_imgproc.findContours(newMat, contours, hierarchy, opencv_imgproc.CV_RETR_TREE, opencv_imgproc.CHAIN_APPROX_NONE, new opencv_core.Point(0, 0));
List<opencv_core.Rect> rectangles = new ArrayList<>();
for (Mat matContour : contours.get()) {
//clueless how to find internal contours here
}
您可以使用与getList相同的样式