我试图在智能合约上调用一个函数。 这是我的CarService.ts代码:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { fromPromise } from 'rxjs/observable/fromPromise';
import { Web3Service } from './web3.service'
declare var require: any;
const carArtifacts = require('../../../build/contracts/Car.json');
const contract = require('truffle-contract');
@Injectable()
export class CarServiceService {
CarContract = contract(carArtifacts);
constructor(private web3ser: Web3Service) {
this.CarContract.setProvider(web3ser.web3.currentProvider)
}
getCarDetail():Observable<any>{
return Observable.create(observer =>{
this.CarContract
.deployed()
.then(instance =>{
console.log('instaaance', instance)
return instance.model.call()
})
.then(value =>{
observer.next(value)
observer.complete()
})
.catch(err =>{
console.log('errrrrrrrrrr', err)
})
})
}
}
这是我的appComponent.ts代码:
import { Component, HostListener, NgZone } from '@angular/core';
import { canBeNumber } from './util/validation';
import { Web3Service } from './services/web3.service'
import { CarServiceService} from "./services/car-service.service"
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
accounts: any;
account: any;
carRes: any;
constructor(
private _ngZone: NgZone,
private web3Service: Web3Service,
private carser: CarServiceService
) {
this.onReady();
}
onReady = () => {
// Get the initial account balance so it can be displayed.
this.web3Service.getAccounts().subscribe(accs => {
this.accounts = accs;
this.account = this.accounts[0];
console.log('accccccouuuuntsss', this.accounts)
this.CarDetail()
}, err => alert(err))
}
CarDetail= ()=>{
this.carser.getCarDetail()
.subscribe(val=>{
this.carRes = val;
console.log('caaaar moddeeel', this.carRes)
}, err => alert(err))
}
}
我只是不明白为什么我在尝试获取汽车细节时仍然遇到错误:TypeError:无法读取未定义的属性'apply' 在Provider.sendAsync
如果有人能帮助我,我会非常感激, 谢谢,
答案 0 :(得分:2)
这是web3 @ 1&amp; amp;的兼容性问题。 truffle-contract@3.0.4。 尝试在创建web3实例之前添加此行:
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
答案 1 :(得分:0)
可以this帮助你。看起来你有不兼容的依赖关系vesions