I`m new to JS and apologize for asking a primary question!
We have this first.js file as a js class example with:
const {MakeRequest} = require("./Request");
let api;
let token;
let unique_token;
function chat(data, unique_token = null) {
try {
if (api != null && token != null) {
return MakeRequest(token, api, data, unique_token)
} else {
return {
"provider": {
"website": "https://example.com",
"source": "Example"
},
"status": true,
"response": [],
"options": {},
"error": {
"code": 207,
"message": "Token or Api token did not valueted!"
}
}
}
} catch (e) {
return {
"provider": {
"website": "https://example.com",
"source": "Example"
},
"status": true,
"response": [],
"options": {},
"error": {
"code": e.code,
"message": e.message
}
}
}
}
module.exports = {
token,api,unique_token,chat
}
also, I have this second.js file as a executable js file:
const object = require("./first.js")
object.token ="123456"
object.api ="123456"
object.token ="123456"
console.log(object.chat("hello"))
If I run the second.js file, the api variable is undefined and didn`t get the value from second.js, how can I resolve this problem without change the second.js codes!
You can't edit a variable from a scope outside where it was declared.
Consider passing the values as arguments to chat function when you call it.
Related
{
"chats": {
"3q7QDEHAVpU3DhiNyAOKlZY7L0S25rAQ6mD63HRiygVytCutjMfyZr43": {
"messages": {
"-MhP9vpwRKLND9tIBBOT": {
"message": "thanks",
"senderId": "5rAQ6mD63HRiygVytCutjMfyZr43",
"timeStamp": 1629305419888
},
"-MhSZ8z1kObr2W0UOdZK": {
"message": "hello",
"senderId": "3q7QDEHAVpU3DhiNyAOKlZY7L0S2",
"timeStamp": 1629362363332
},
}
},
"3q7QDEHAVpU3DhiNyAOKlZY7L0S2Ud9F8Ke4bZTMu9vMf5GF98jnwng2":
{},
"5rAQ6mD63HRiygVytCutjMfyZr433q7QDEHAVpU3DhiNyAOKlZY7L0S2":
{},
}
}
I want to retrieve data from the real time database but due to unnamed nodes I am not able to capture the data. Please help me?
When I run the function I getting null in console.log:
Path is correct but snapshot shown null in console:
snapshot: null
function selectAllData6() {
var Id1 = this.id;
var Path1 = firebase.database().ref("chats/{senderId}/messages/{receiverId}/message");
Path1.on('value', function(snapshot) {
var message = snapshot.val();
console.log("message: " + message);
console.log("Path1: " + Path1);
console.log("snapshot: " + snapshot.val());
});
}
selectAllData6();
I am trying to make sense of how the fulfillment works, and I cannot get the responses from the if statements to work. Whenever I write the keyword, the default response I get is Not available.
The webhook for the intent is enabled, the entity is 'hooked' in the intent as well.
What am I missing here?
const functions = require('firebase-functions');
const { dialogflow } = require('actions-on-google');
const app = dialogflow();
const WELCOME_INTENT = 'Default Welcome Intent';
const USER_MESSAGE_ENTITY = 'UserMessage';
app.intent(WELCOME_INTENT, (conv) => {
const userMessage = conv.parameters(USER_MESSAGE_ENTITY).toLowerCase();
if (userMessage == 'hey') {
conv.ask('Hey there');
} else if (userMessage == 'greetings') {
conv.ask('Greetings, how are you');
} else if (userMessage == 'evening') {
conv.ask('Good evening');
}
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
{
"responseId": "8499a8f2-b570-4fb2-9f3c-262bd03db01e-c4f60134",
"queryResult": {
"queryText": "hey",
"action": "input.welcome",
"parameters": {
"UserMessage": "hey"
},
"allRequiredParamsPresent": true,
"intent": {
"name": "projects/wandlee-zad-rekrutacyjne--euol/agent/intents/d76ffc6c-c724-4fa4-8c9b-7178a2d7f9b7",
"displayName": "Default Welcome Intent"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 76
},
"languageCode": "pl",
"sentimentAnalysisResult": {
"queryTextSentiment": {
"score": 0.2,
"magnitude": 0.2
}
}
},
"webhookStatus": {
"code": 14,
"message": "Webhook call failed. Error: UNAVAILABLE."
}
}
I don't know where you got conv.parameters(USER_MESSAGE_ENTITY).
The parameters of the intent are accessible as a second function argument. It is going to be a map:
app.intent(WELCOME_INTENT, (conv, params) => {
const userMessage = params[USER_MESSAGE_ENTITY].toLowerCase();
// ...
})
``
I've created a vscode extension which handle some Rakefile tasks.
Now I want to limit that the extension will be displayed in the explorer only when user
open a Rakefile
for that I use the following entry in the package.json
"contributes": {
"views": {
"explorer": [
{
"id": "rakeView",
"name": "Rakee",
"when": "resourceFilename ='Rakefile'"
}
]
},
"commands": [
{
"command": "rakeView.executeTask",
"title": "Execute",
"icon": "images/icon.png"
}
],
when adding the following line
"when": "resourceFilename ='Rakefile'"
the extension removed from the explorer view, when I remove it I was able to see the extension.
I want it to display only when a project have a Rakefile how could I do it ?
https://github.com/microsoft/vscode-extension-samples/tree/1aae138e311fb87cc3ed2782be287f5d2f78e327/task-provider-sample
update
After trying the answer below it's still not working, this is the all code:
import * as vscode from "vscode";
import { rakeTaskProvider } from "./rakeCmd";
import { TaskTreeDataProvider } from "./rakeView";
let rakeTaskProvider: vscode.Disposable | undefined;
export function activate(_context: vscode.ExtensionContext): void {
const workspaceRoot = vscode.workspace.rootPath;
const onChangeActiveTextEditor = () => {
let editor = vscode.window.activeTextEditor;
vscode.commands.executeCommand('setContext', 'rakeView:fileIsRake', editor && editor.document.languageId === 'rakefile');
};
vscode.window.onDidChangeActiveTextEditor(onChangeActiveTextEditor, null, _context.subscriptions);
onChangeActiveTextEditor();
rakeTaskProvider = vscode.tasks.registerTaskProvider(
rakeTaskProvider.rakeType,
new rakeTaskProvider(workspaceRoot)
);
vscode.window.registerTreeDataProvider(
"rakeView",
new TaskTreeDataProvider(_context)
);
vscode.commands.registerCommand("rakeView.executeTask", function (task) {
console.log(task);
vscode.tasks.executeTask(task).then(
function (value) {
return value;
},
function (e) {
console.error(
"error",
e
);
}
);
});
}
export function deactivate(): void {
if (rakeTaskProvider) {
rakeTaskProvider.dispose();
}
}
package.json
"contributes": {
"views": {
"explorer": [
{
"id": "rakeView",
"name": "Rakke",
"when": "rakeView:fileIsRake"
}
]
},
You need to use the context, when the editor changes set a context variable to signal the current file is a Rakefile
In your activate function
const onChangeActiveTextEditor = () => {
let editor = vscode.window.activeTextEditor;
vscode.commands.executeCommand('setContext', 'myExtenstion:fileIsRake', editor && editor.document.languageId === 'rake');
};
vscode.window.onDidChangeActiveTextEditor(onChangeActiveTextEditor, null, context.subscriptions);
onChangeActiveTextEditor();
In package.json add this context variable
"views": {
"explorer": [
{
"id": "rakeView",
"name": "Rakee",
"when": "myExtenstion:fileIsRake"
}
]
},
I am using apollo to get my data in a Nuxt.js project, using asyncData
import homeQuery from '~/apollo/queries/home'
export default {
async asyncData({app}) {
const homeresult = await app.apolloProvider.defaultClient.query({
query: homeQuery
})
return { home: homeresult.data.home }
},
data () {
return {
home: {}
}
}
this works fine when the result of the query is an object, for example the above is:
{
"data": {
"home": {
title": "Home"
}
}
}
However, if the query result is an array:
{
"data": {
"home": [
{
"id": "1"
},
{
"id": "2"
},
{
"id": "3"
}
]
}
}
nothing gets returned. (I also tested data () { return { home: [] } } )
Do I have to treat arrays differently, and how should I correctly write the asyncData?
I have absolutely no idea what changed... but when I tried again that code, I had no more problems with arrays as results.
I am getting this a TypeError: Cannot read property '0' of undefined when testing my aws lambda. The code is
'use strict';
const snsPublisher = require('./snsPublisher');
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
const message = event.Records[0].Sns.Message;
console.log('From SNS:', message);
callback(null, message);
};
module.exports.snsLamdbaTriggered = (event, context, callback) => {
var topic = event.Records[0].Sns.TopicArn;
var message = event.Records[0].Sns.Message;
console.log(topic + ' ' + message);
callback(null, { message: 'SNS lamdba was triggered from the topic ' + topic + ' with message ' + message , event });
};
When I change the value inside the brackets I still continue to get the same error. Does anyone know where the problem may lie?
When invoking a lambda function using SNS, the function expects an event object that has specific attributes. So, "testing" your lambda function would fail unless you configure your lambda test event to look exactly the same as an SNS event, for example:
{
"Records": [
{
"EventVersion": "1.0",
"EventSubscriptionArn": eventsubscriptionarn,
"EventSource": "aws:sns",
"Sns": {
"SignatureVersion": "1",
"Timestamp": "1970-01-01T00:00:00.000Z",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
},
"Type": "Notification",
"UnsubscribeUrl": "EXAMPLE",
"TopicArn": topicarn,
"Subject": "TestInvoke"
}
}
]
}