我不明白这里有什么特别的。我试图简单地加载一个名为asset的类的所有对象,但是当我尝试保存结果时,我不断收到此错误。
asset.service.ts(24,23):提供的参数与调用目标的任何签名都不匹配
我有以下代码
assets.component.ts
export class AssetsComponent implements OnInit {
constructor(private assetService: AssetService, private errorService: ErrorService) { }
assets: Asset[] = [];
ngOnInit() {
this.assetService.getAssets()
.subscribe(
assets => {
console.log('assets is', assets);
this.assets = assets;
this.assetService.assets = assets;
},
error => this.errorService.handleError(error)
)
}
}
asset.service.ts
@Injectable()
export class AssetService {
@Output() asset: Asset;
assets: Asset[] = [];
constructor(private _http: Http) { }
getAssets() {
return this._http.get('http://localhost:3000/asset')
.map(response => {
const data = response.json().obj;
console.log('getAssets data from response is');
console.log(data);
let objs: any[] = [];
for (let i = 0; i < data.length; i++) {
console.log('Asset Data here is', data[i]);
let asset = new Asset(data[i].name, data[i].mimetype, data[i].filename, data[i].local_path, data[i].size);
objs.push(asset);
}
return objs;
})
.catch(error => Observable.throw(error.json()));
}
}
在我的 assets.js 路线中(在使用快递的节点中)我有
router.get('/', function(req, res, next) {
Asset.find()
//.populate('name', 'filename')
.exec(function(err, docs) {
if (err) {
return res.status(404).json({
title: 'An Error occured',
error: err
});
}
console.log('node get docs are');
console.log(docs);
res.status(200).json({
asset: 'Success',
obj: docs
});
});
});
我在Angular2中的asset.ts模型看起来像:
export class Asset {
name: string;
mimetype: string;
filename: string;
local_path: string;
size: number;
s3loc: string;
thumb_local_path: string;
proxy_local_path: string;
thumb_s3_path: string;
proxy_s3_path: string;
... constructor follows...
答案 0 :(得分:0)
所以这就像预期的那样简单,但我在搜索时没有看到这个答案。即使我知道这个特定的getAssets调用何时运行,它通常不会在响应中返回所有字段,为此工作,所有字段必须在新资产部分中计算对于在资产服务中创建的对象。
所以上面一行:
let asset = new Asset(data[i].name, data[i].mimetype, data[i].filename, data[i].local_path, data[i].size);
实际上必须是
let asset = new Asset(data[i].name, data[i].mimetype, data[i].filename, data[i].local_path, data[i].size, data[i].s3loc, data[i].thumb_local_path, data[i].proxy_local_path, data[i].thumb_s3_path, data[i].proxy_s3_path);