在将我在回调中收到的dat.id分配给我的AccuprobeID变量时遇到了麻烦,以后我会用它来进行另一个API调用。任何帮助,将不胜感激。
我尝试将sap.deviceInfo(callback)
更改为sap.deviceInfo().then(res=>{})
,但这没用:/
<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>
答案 0 :(得分:1)
您需要在外部this
处添加新变量ref,因为回调函数ref中的this
到全局。
addClaim(claimCode){
var self = this;
sap.deviceInfo(function callback(err, dat) {
if (err) { throw err; }
console.log("Device ID: %s, claimed: %s", dat.id, dat.claimed ? "yes" : "no");
self.AccuprobeID = dat.id
});
sap.setClaimCode(claimCode, function callback(err, dat) {
if(err) { throw err; }
});
this.claimCodeSet = true
}
否则,请使用箭头功能直接访问this
。
addClaim(claimCode){
sap.deviceInfo((err, dat) => {
if (err) { throw err; }
console.log("Device ID: %s, claimed: %s", dat.id, dat.claimed ? "yes" : "no");
this.AccuprobeID = dat.id
});
sap.setClaimCode(claimCode, (err, dat) => {
if(err) { throw err; }
});
this.claimCodeSet = true
}
答案 1 :(得分:0)
您需要使用在AccuprobeID
中声明的data
。要使用它,您需要执行this.AccuprobeID
。
这是代码:
<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){
sap.deviceInfo(callback);
function callback(err, dat) {
if (err) { throw err; }
console.log("Device ID: %s, claimed: %s", dat.id, dat.claimed ? "yes" : "no");
this.AccuprobeID=dat.id;
};
sap.setClaimCode(claimCode,callback);
function callback(err, dat) {
if(err) { throw err; }
}
this.claimCodeSet=true;
},
claimDeviceToProduct(){
console.log(this.AccuprobeID)
particle.addDeviceToProduct({product:'2121', deviceId: this.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>