Nuxt constant is assigned a value but never used - vuejs2

I have an Nuxt (Vue2) application which throws me an error:
c:\wamp64-3-2-0\www\test\dynamic_stores\campaign.js
1436:12 error 'campaign' is assigned a value but never used
no-unused-vars 1438:12 error 'campaign' is assigned a value but
never used no-unused-vars 1441:18 error 'campaign' is not defined
no-undef
if( share_token ) {
const campaign = await this.$api.campaigns.getSharedCampaign(share_token);
} else {
const campaign = await this.$api.campaigns.getCampaign(state.id);
}
const data = campaign.data;
Can somebody tell me please what is wrong with this code? Thanks.

You need to declare campaign outside of the if-else statement in order to access it outside of the if-else statement.
Example:
let campaign;
if (share_token) {
campaign = await this.$api.campaigns.getSharedCampaign(share_token);
} else {
campaign = await this.$api.campaigns.getCampaign(state.id);
}
const data = campaign.data;

If it's a method, you can return API calls and you can use response as a result.
handleFetch(){
if(share_token) {
return this.$api.campaigns.getSharedCampaign(share_token);
} else {
return this.$api.campaigns.getCampaign(state.id);
}
}
thank you can use in template or somewhere else with:
handleFetch()

Related

How to know when monitorCharacteristic is done with react native ble plx?

I use react-native-ble-plx in my project to communicante with my tool, and I'm trying to monitor one of its characteristics.
The result is rather long so the monitoringCharacteristic function is looping until it has send me everything, but I don't know how to be sure that the loop is done.
Here is my monitoring function :
const scanMyDevice = async (device) => {
const serviceUUID = '569a1-****'
const writeUUID = '569a2-****'
const readUUID = '569a2-****'
await device.discoverAllServicesAndCharacteristics()
await device.characteristicsForService(serviceUUID)
await device.writeCharacteristicWithResponseForService(serviceUUID, writeUUID, 'IzEwCg==')
var tempTrame = ''
const subscription = device.monitorCharacteristicForService(serviceUUID, readUUID, (error, a) => {
if (error) {
console.log(error)
console.log(error.message)
}
else {
tempTrame = tempTrame + base64.decode(a.value)
setTrameResult(tempTrame)
console.log('// INSIDE ///', tempTrame)
}
}, 'idMonitor')
return () => {
subscription.remove();
}
console.log('DONE')
}
In my code, 'DONE' is going to be printed before 'INSIDE trame'.
I tried to put console.log('DONE') inside the return, but then it was never printed.
I tried to put .then( console.log('DONE')) just before return but it sent me an error saying that it has nothing to do here...
Can someone help me please ?
Finally I went around the problem by defining an ending pattern to the info I wanted to send/read via BLE, and then I wrote a loop to continue reading while the ending pattern was not encountered:
let tempTrame = tempTrame + base64.decode(a.value)
let finalTrame = ''
// check if my variable has the end symbols (to be sure that I have all the information) and if it is the first time I get this information
if (tempTrame.includes('#109F')) {
stop = true
// suppress end symbols
tempTrame = tempTrame.replace(endTrame, '')
finalTrame = tempTrame
}
console.log(finalTrame)

how to load new data from api using RefreshIndicator

Future<void> _fetchPage(int pageKey) async {
try {
final newItems = await ApiServices.fetchArticleList(pageKey, _pageSize);
final isLastPage = newItems.length < _pageSize;
if (isLastPage) {
_pagingController.appendLastPage(newItems);
} else {
final nextPageKey = pageKey + newItems.length;
_pagingController.appendPage(newItems, nextPageKey);
}
} catch (error) {
print(error);
_pagingController.error = error;
}
}
so, i have method _fetchPage() that loads data from api initially, but on refresh new data are not updated in screen. Any help will be appreciated.
change your onRefresh callback to
onRefresh:()=>_fetchPage(0)
_fetchPage method in your code is expecting a parameter but you are not passing the value. since the onRefresh callback doesn't have any value to pass as parameter to the function

How to count the number of elements in this particular object

I am making a call to an API and the response is somehow what I expect. However, I want to count the number of elements returned and I can not do it. This is what I think is important from the code.
Call in Vue component
data(){
return {
messages: {}
}
},
loadMessages(){
axios.get("api/messagesmenu")
.then((data) => { this.messages = data.data})
}
Api controller
public function index(){
$messages = Message::all()->where('read_at', NULL);
if(isset($messages)){
foreach($messages as $message){
$from = User::find($message->from_id);
$message->fromPrenom = $from->first_name;
$message->fromNom = $from->last_name;
$message->fromImage = $from->user_image;
}
}else{
$messages = [];
}
return $messages;
}
Type of response from the API
{"3":{"id":560,"from_id":2,"to_id":1,"content":"tgr","created_at":"2019-07-15 16:59:03","read_at":null,"fromPrenom":"abdel1","fromNom":"Hidalgo","fromImage":"user2-160x160.png"}}
I want to count the number of objects I obtain. if (in vue component) I do
this.messages.length
it returns undefined
Try this:
const messages = {"3":{"id":560,"from_id":2,"to_id":1,"content":"tgr","created_at":"2019-07-15 16:59:03","read_at":null,"fromPrenom":"abdel1","fromNom":"Hidalgo","fromImage":"user2-160x160.png"}}
console.log(Object.keys(messages).length) // 1
Or in your code:
...
.then((data) => {
this.messages = data.data
console.log(Object.keys(this.messages).length)
})

How to tell if html video is currently playing

I'm trying to test if an Html video is currently playing, but can't seem to figure out how to get the currentTime. I've been trying things like:
async videoIsPlaying(indexOfVideo = 0) {
return ClientFunction(() => {
const video = document.getElementsByTagName('video')[indexOfVideo];
return video.currentTime > 0;
});
}
but my expect:
await t.expect(await playerPage.videoIsPlaying()).eql(true);
is returning:
AssertionError: expected [Function: __$$clientFunction$$] to deeply equal true
What am I doing wrong? Also, I'm using .eql() because .ok() returns truthy for any result.
Ahhh... just needed to fire the function, and also pass in the index thusly...
async videoIsPlaying(indexOfVideo = 0) {
return await ClientFunction((indexOfVideo) => {
const video = document.getElementsByTagName('video')[indexOfVideo];
return video.currentTime > 0;
})(indexOfVideo);
}
Just FYI, this function lives in a page object

Get JavaScript Array of Objects to bind to .Net Core List of ViewModel

I have a JS Array of Objects which, at time of Post contains three variables per object:
ParticipantId,
Answer,
ScenarioId
During post, there is an Array the size of 8 (at current anyway) which all correctly contain data. When I call post request, the Controller does get hit as the breakpoint triggers, the issue is when I view the List<SurveyResponse> participantScenarios it is shown as having 0 values.
The thing I always struggle to understand is that magic communication and transform between JS and .Net so I am struggling to see where it is going wrong.
My JS Call:
postResponse: function () {
var data = JSON.stringify({ participantScenarios: this.scenarioResponses})
// POST /someUrl
this.$http.post('ScenariosVue/PostScenarioChoices', data).then(response => {
// success callback
}, response => {
// error callback
});
}
My .Net Core Controller
[HttpPost("PostScenarioChoices")]
public async Task<ActionResult> PostScenarioChoices(List<SurveyResponse> participantScenarios)
{
List<ParticipantScenarios> addParticipantScenarios = new List<ParticipantScenarios>();
foreach(var result in participantScenarios)
{
bool temp = false;
if(result.Answer == 1)
{
temp = true;
}
else if (result.Answer == 0)
{
temp = false;
}
else
{
return StatusCode(400);
}
addParticipantScenarios.Add(new ParticipantScenarios
{
ParticipantId = result.ParticipantId,
Answer = temp,
ScenarioId = result.ScenarioId
});
}
try
{
await _context.ParticipantScenarios.AddRangeAsync(addParticipantScenarios);
await _context.SaveChangesAsync();
return StatusCode(201);
}
catch
{
return StatusCode(400);
}
}