Having trouble with assigning the dat.id received in a callback to my AccuprobeID variable, which is used for another API call later on. Any assistance would be greatly appreciated.
I attempted changing sap.deviceInfo(callback)
to
sap.deviceInfo().then(res=>{})
but unfortunately that did not work :/
<template>
<div class="home">
<b-button v-if="!claimCode.length>0" @click="createClaim">claim</b-button>
<div v-if="claimCode.length>0 && !claimCodeSet">
<p>Please Switch Network to Photon </p>
<b-button variant="primary" @click="addClaim(claimCode)">Add Claim Code to Accuprobe</b-button>
</div>
<div v-if="claimCodeSet">
Please Switch Back to Normal Network
<b-button @click="claimDeviceToProduct">Claim to Account</b-button>
</div>
</div>
</template>
<script>
import axios from 'axios'
var SoftAPSetup = require('softap-setup');
var sap = new SoftAPSetup();
var Particle = require('particle-api-js');
var particle = new Particle();
var token;
export default {
data(){
return{
claimCode:'',
claimCodeSet:false,
AccuprobeID:''
}
},
name: 'home',
components: {
},
methods:{
createClaim(){
axios.post('https://api.particle.io/v1/products/2121/device_claims/?access_token='+this.$store.state.user.PToken)
.then((res)=>{
console.log(res)
this.claimCode=res.data.claim_code
console.log(this.claimCode)})
},
addClaim(claimCode,AccuprobeID){
sap.deviceInfo(callback);
function callback(err, dat) {
if (err) { throw err; }
console.log("Device ID: %s, claimed: %s", dat.id, dat.claimed ? "yes" : "no");
AccuprobeID=dat.id
return AccuprobeID
};
sap.setClaimCode(claimCode,callback);
function callback(err, dat) {
if(err) { throw err; }
}
this.claimCodeSet=true
},
claimDeviceToProduct(AccuprobeID){
console.log(AccuprobeID)
particle.addDeviceToProduct({product:'2121', deviceId: AccuprobeID, auth: this.$store.state.user.Bear }).then(function(data) {
console.log('device claim data:', data);
}, function(err) {
console.log('device claim err:', err);
});
},
}}
</script>