0

I am trying to call a function on a smart contract. Here is my CarService.ts code:

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)
  })
  })
  }

  }

And Here is My appComponent.ts Code:

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))
  }
 }

I just dont get it why i keep having that error when trying to get the car details : TypeError: Cannot read property 'apply' of undefined at Provider.sendAsync

If anyone could help me with it i would really appreciate it , Thank you,

1 Answer 1

3

It's a compatibility issue between web3@1 & [email protected]. Try adding this line before creating an instance of web3 :

Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.