类型“ ArrayBuffer”不可分配给“字符串”类型

时间:2018-12-12 20:47:12

标签: javascript angular angular6

使用angular6和node.js的应用程序遇到了一个问题,该问题现在正在编写负责在服务器上上传图片的部分,创建了imagePreview =“”变量(是的,默认情况下为空)

dBN$Year<-"2017" #all data are from 2017 ##I could not figure out how jDay was formatted, so I created a manual 'key' ##to get Mo and Day by counting from a known date/jDay pair in original data #Example: # 13635 is Mo=5 Day=1 # 13665 is Mo=5 Day=31 # 13666 is Mo=6 Day=1 # 13695 is Mo=6 Day=30 key4<-data.frame("jDay"=c(13633:13634), "Day"=c(29:30), "Mo"=4) key5<-data.frame("jDay"=c(13635:13665), "Day"=c(1:31), "Mo"=5) key6<-data.frame("jDay"=c(13666:13695), "Day"=c(1:30), "Mo"=6) key7<-data.frame("jDay"=c(13696:13719), "Day"=c(1:24), "Mo"=7) #make master 'key' key<-rbind(key4,key5,key6,key7) # Merge 'key' with dataset so all rows now have 'Mo' and 'Day' values dBM<-merge(dBN, key, by="jDay", all.x=TRUE) #clean unecessary columns and rename 'Mo' and 'Day' so it matches PC17 dataset dBM<-dBM[ , -c(2,3,6:16)] colnames(dBM)[5:6]<-c("Day","Mo") #I noticed an issue with duplication - merge with PC17 created a massive dataframe dBM %>% ### Have too many observations per day, will duplicate merge out of control. count(SiteID, jDay, DayL50) %>% summarise( min=min(n, na.rm=TRUE), mean=mean(n, na.rm=TRUE), max=max(n, na.rm=TRUE) ) ## to fix this I only kept distinct observations so that each day has 1 observation dB<-distinct(dBM, .keep_all = TRUE) ### Now run above line again to check how many observations per day are left. Should be 1

这就是我在第74行上显示的内容,我不知道错误在哪里(WebStorm强调> PCdB<-(merge(PC17, dB, by=c("SiteID", "Year","Mo","Day"), all.x=TRUE, all=FALSE,no.dups=TRUE)) > ### all.x=TRUE is important. This keeps all PC17 data, even stuff that DOESNT have dB data that corresponds to it. > library(dplyr) #Here is the NA interpolated 'dB' dataset > dB %>% + group_by(SiteID) %>% + dplyr::summarise( + min_dBL50=min(DayL50, na.rm=TRUE), + max_dBL50=max(DayL50, na.rm=TRUE), + mean_dBL50=mean(DayL50, na.rm=TRUE), + med_dBL50=median(DayL50, na.rm=TRUE) + ) # A tibble: 59 x 5 SiteID min_dBL50 max_dBL50 mean_dBL50 med_dBL50 <chr> <dbl> <dbl> <dbl> <dbl> 1 CU1D 44.7 53.1 49.4 50.2 2 CU1M 37.6 65.2 59.5 62.6 3 CU1U 35.5 51 43.7 44.8 4 CU2D 42 52 47.8 49.3 5 CU2M 38.2 49 43.1 42.9 6 CU2U 34.1 53.7 46.5 47 7 CU3D 46.1 53.3 49.7 49.4 8 CU3M 44.5 73.5 61.9 68.2 9 CU3U 42 52.6 47.0 46.8 10 CU4D 42 45.3 44.0 44.6 # ... with 49 more rows # Now here is the PCdB merged dataset, and we are no longer missing values! > PCdB %>% + group_by(SiteID) %>% + dplyr::summarise( + min_dBL50=min(DayL50, na.rm=TRUE), + max_dBL50=max(DayL50, na.rm=TRUE), + mean_dBL50=mean(DayL50, na.rm=TRUE), + med_dBL50=median(DayL50, na.rm=TRUE) + ) # A tibble: 60 x 5 SiteID min_dBL50 max_dBL50 mean_dBL50 med_dBL50 <chr> <dbl> <dbl> <dbl> <dbl> 1 CU1D 44.8 50 46.8 47 2 CU1M 59 63.9 62.3 62.9 3 CU1U 37.9 46 43.6 44.4 4 CU2D 42.1 51.6 45.6 44.3 5 CU2M 38.4 48.3 44.2 45.5 6 CU2U 39.8 50.7 45.7 46.4 7 CU3D 46.5 49.5 47.7 47.7 8 CU3M 67.7 71.2 69.5 69.4 9 CU3U 43.3 52.6 48.1 48.2 10 CU4D 43.2 45.3 44.4 44.9 # ... with 50 more rows

ERROR in src/app/categories-page/categories-form/categories-form.component.ts(74,7): error TS2322: Type 'string | ArrayBuffer' is not assignable to type 'string'.
[1]   Type 'ArrayBuffer' is not assignable to type 'string'.

请告诉我如何解决

负责下载图像的组件的代码:

reader.result

创建和编辑

reader.onload = () => {
      this.imagePreview = reader.result
    }

1 个答案:

答案 0 :(得分:1)

ArrayBuffer作为字符串可能会导致数据截断(必须测试)。
因此,您必须显式转换它,以告诉编译器“ 我知道我在做什么”。

尝试一下,它对我有用:

reader.onload = () => {
    this.imagePreview = reader.result as string;
}