I don't understand how can I write the file like this:
{
"726623241984278589": {
"322366672625467392": {
"Moderation": "Kick",
"Target": "Bobo#1601",
"Moderator": "Bobosky#3914",
"Reason": "Testing",
"ID": "#XLR6RV8M"
}
}
}
Here is the my code of fs.writeFile:
const TargetMemberID = TargetMember.id
data[message.guild.id] = {
TargetMemberID: {
Moderation: 'Kick',
Target: `${TargetMember.user.tag}`,
Moderator: `${message.author.tag}`,
Reason: `${Reason}`,
ID: `#${ID}`
}
}
fs.writeFile("./data.json", JSON.stringify(data), (err) => {
if (err) console.log(err);
});
but it out put like this:
{
"726623241984278589": {
"TargetMemberID": {
"Moderation": "Kick",
"Target": "Bobo#1601",
"Moderator": "Bobosky#3914",
"Reason": "Testing",
"ID": "#XLR6RV8M"
}
}
}
so this is not what I expected to see. I want to see the TargetMemberID above replaced by the number just like what I expected as the first json code.
Any clue on it?
I've tried making the writing part as
data[message.guild.id[TargetMember.id]]
but it doesn't work.
PS : I didn't put all my codes such as the definding part of TargetMember, Reason etc. All of those are based on discord.js.
In JavaScript, object keys are strings, so when you write TargetMemberID as key, it's the same as writing "TargetMemberID". If you want the key to be the value of TargetMemberID, you need to add square brackets around it. And this way you could also just use TargetMember.id, you don't need a new variable for this.
Also, you don't need to use template literals in those values, you can just pass them as below.
data[message.guild.id] = {
[TargetMember.id]: {
Moderation: 'Kick',
Target: TargetMember.user.tag,
Moderator: message.author.tag,
Reason: Reason,
ID: ID,
}
}
You could also use the shorthand syntax, when the object key and the variable is the same:
data[message.guild.id] = {
[TargetMember.id]: {
Moderation: 'Kick',
Target: TargetMember.user.tag,
Moderator: message.author.tag,
// same as Reason: Reason
Reason,
// same as ID: ID
ID,
}
}
Related
I've been dealing with a project. My goal is to get result from search engine in all circumstances for example, although i enter a keyword which is not include the keys inside data or is a empty string, I still need to get some result.How can i reach my goal?
you can see the query below :
query: {
regexp: {
title: "something to not found .*",
},
Try use "prefix" or "query_string"
You also can use title.keyword for exact value
1 -
{
"query": {
"prefix": {
"title": {
"value": "<data>"
}
}
}
}
2 -
{
"query": {
"query_string": {
"default_field": "title",
"query": "<data>*^0"
}
}
}
While automating a website, I have a requirement to run a test case(it block) multiple times with different set of testdata in cypress.
Please consider the below example :
it('example test', () => {
//first run
getOnDefaultForm.typeUserName('Name1');
getOnDefaultForm.typePassword('Pass1');
getOnDefaultForm.clickSubmit();
//second run
getOnDefaultForm.typeUserName('Name2');
getOnDefaultForm.typePassword('Pass2');
getOnDefaultForm.clickSubmit();
//third run
getOnDefaultForm.typeUserName('Name3');
getOnDefaultForm.typePassword('Pass3');
getOnDefaultForm.clickSubmit();
});
How can I achieve this in Cypress?
I think you need to have a look as this repo: https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__dynamic-tests Or just search this site, this is not the first time someone has asked this very question.
In general, you can wrap your it in a loop. In practice, it'd look e.g. like this:
const testData = [
{
name: 'Name1',
password: 'Pass1'
},
{
name: 'Name2',
password: 'Pass2'
},
{
name: 'Name3',
password: 'Pass3'
}
]
testData.forEach((credentials) => {
it('example test', () => {
getOnDefaultForm.typeUserName(credentials.name);
getOnDefaultForm.typePassword(credentials.password);
getOnDefaultForm.clickSubmit();
})
});
It is also possible to put the data in a json file in the fixtures folder, and import it at the top of the spec file.
This methodology is still working in Cypress version 12.5.0.
fixture
[
{ "name": 'Name1', "password": 'Pass1' },
{ "name": 'Name2', "password": 'Pass2' },
{ "name": 'Name3', "password": 'Pass3' }
]
test
const testData = require('../fixtures/test-data.json')
testData.forEach((credentials) => {
it('example test for ' + credentials.name, () => {
getOnDefaultForm.typeUserName(credentials.name);
getOnDefaultForm.typePassword(credentials.password);
getOnDefaultForm.clickSubmit();
})
})
I'm learning FQL and trying to do a mass update, but I can't figure out what I'm doing wrong, nor can I really figure out what the error is really pointing to.
Here is my code:
const updateResult = await serverClient.query(
q.Map(
guests,
q.Lambda(
"guest",
q.Update(q.Var("guest").id, {
data: {
emailSent: q.Var("guest").emailSent,
emailStatus: q.Var("guest").emailStatus,
emailRejectReason: q.Var("guest").emailRejectReason,
},
})
)
)
);
Here is the what the guests object is via console.log:
[ { email: 'myemail+bart72320#gmail.com',
emailStatus: 'sent',
emailRejectReason: null,
emailSent: true,
id: Ref(Collection("Guests"), "271884343706649107") } ]
Here is that same object with JSON.stringify:
[
{
"email": "myemail+bart72320#gmail.com",
"emailStatus": "sent",
"emailRejectReason": null,
"emailSent": true,
"id": {
"#ref": {
"id": "271884343706649107",
"collection": {
"#ref": {
"id": "Guests",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
}
}
]
Here is part of the error that is returned:
{ [BadRequest: invalid expression]
name: 'BadRequest',
message: 'invalid expression',
description:
'No form/function found, or invalid argument keys: { params }.',
requestResult:
RequestResult {
method: 'POST',
path: '',
query: null,
requestRaw:
'{"map":{"lambda":"guest","expr":{"params":{"object":{"data":{"object":{}}}}}},"collection":[{"object":{"email":"myemail+bart72320#gmail.com","emailStatus":"sent","emailRejectReason":null,"emailSent":true,"id":{"#ref":{"id":"271884343706649107","collection":{"#ref":{"id":"Guests","collection":{"#ref":{"id":"collections"}}}}}}}}]}',
requestContent: Expr { raw: [Object] },
responseRaw:
'{"errors":[{"position":["map","expr"],"code":"invalid expression","description":"No form/function found, or invalid argument keys: { params }."}]}',
I've gotten updates to work and lambdas to work, but not this one and maybe I'm just bad at reading very nested, functional looking error messages. What is a form and is it the same as a function, or am I missing keys: params? I don't know what to make of this.
Please help me understand what I'm doing wrong and if this error message is actually helpful and how to interpret it or if it's just a confusing catchall?
Thanks!
You are mixing Javascript syntax with FQL expressions.
Var("guest")
is a FQL expression, but
Var("guest").id
is a Javascript syntax. The equivalent of dot operator in FQL is
Select("id", Var("guest"))
Remember that FQL is not executed on Javascript, but is executed on server side.
I'm using Dynamoose to simplify my interactions with DynamoDB in a node.js application. I'm trying to write a query using Dynamoose's Model.query function that will search a table using an index, but it seems like Dynamoose is not including all of the info required to process the query and I'm not sure what I'm doing wrong.
Here's what the schema looks like:
const UserSchema = new dynamoose.Schema({
"user_id": {
"hashKey": true,
"type": String
},
"email": {
"type": String,
"index": {
"global": true,
"name": "email-index"
}
},
"first_name": {
"type": String,
"index": {
"global": true,
"name": "first_name-index"
}
},
"last_name": {
"type": String,
"index": {
"global": true,
"name": "last_name-index"
}
}
)
module.exports = dynamoose.model(config.usersTable, UserSchema)
I'd like to be able to search for users by their email address, so I'm writing a query that looks like this:
Users.query("email").contains(query.email)
.using("email-index")
.all()
.exec()
.then( results => {
res.status(200).json(results)
}).catch( err => {
res.status(500).send("Error searching for users: " + err)
})
I have a global secondary index defined for the email field:
When I try to execute this query, I'm getting the following error:
Error searching for users: ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.
Using the Dynamoose debugging output, I can see that the query winds up looking like this:
aws:dynamodb:query:request - {
"FilterExpression": "contains (#a0, :v0)",
"ExpressionAttributeNames": {
"#a0": "email"
},
"ExpressionAttributeValues": {
":v0": {
"S": "mel"
}
},
"TableName": "user_qa",
"IndexName": "email-index"
}
I note that the actual query sent to DynamoDB does not contain KeyConditions or KeyConditionExpression, as the error message indicates. What am I doing wrong that prevents this query from being written correctly such that it executes the query against the global secondary index I've added for this table?
As it turns out, calls like .contains(text) are used as filters, not query parameters. DynamoDB can't figure out if the text in the index contains the text I'm searching for without looking at every single record, which is a scan, not a query. So it doesn't make sense to try to use .contains(text) in this context, even though it's possible to call it in a chain like the one I constructed. What I ultimately needed to do to make this work is turn my call into a table scan with the .contains(text) filter:
Users.scan({ email: { contains: query.email }}).all().exec().then( ... )
I am not familiar with Dynamoose too much but the following code below will do an update on a record using node.JS and DynamoDB. See the key parameter I have below; by the error message you got it seems you are missing this.
To my knowledge, you must specify a key for an UPDATE request. You can checks the AWS DynamoDB docs to confirm.
var params = {
TableName: table,
Key: {
"id": customerID,
},
UpdateExpression: "set customer_name= :s, customer_address= :p, customer_phone= :u, end_date = :u",
ExpressionAttributeValues: {
":s": customer_name,
":p": customer_address,
":u": customer_phone
},
ReturnValues: "UPDATED_NEW"
};
await docClient.update(params).promise();
I am using elasticsearch (managed instance from searchly) with the elasticsearch-js npm client library. I want to search multiple fields in my index from a term. There seems to be loads of documentation on this, e.g.
GET /_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "War and Peace" }},
{ "match": { "author": "Leo Tolstoy" }}
]
}
}
}
where I could just set the same value for author and title. However, this is a get request and is structured differently to the nodejs library, where I am doing this:
this._client.search({
index: 'sample',
body: {
query: {
match: {
name: 'toFind'
}
}
}
}).then(function (resp) {
var hits = resp.hits.hits;
}, function (err) {
console.trace(err.message);
});
I can't have multiple match: fields otherwise tsc complains about strict mode, and if I try something like:
query: {
match: {
name: 'toFind',
description: 'toFind'
}
}
then I get an error:
"type": "query_parsing_exception",
"reason": "[match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its \u0027options\u0027 form, with \u0027query\u0027 element?",
Since you want to match same string on multiple fields you need multi match query. Try something like this
this._client.search({
index: 'sample',
body: {
query: {
multi_match: {
query: 'toFind',
fields: ['name','description']
}
}
}
}).then(function (resp) {
var hits = resp.hits.hits;
}, function (err) {
console.trace(err.message);
});