From .NET backend we receive by GET method to Google Chrome an object
Delivery {
Id: 1,
ShippingDateTime: null,
ApproximateHandoverDateTime: null,
Charge: 527,
Cost: null
}
If we look at the preview of GET method Delivery.Charge is 527.
BUT if we type console.log(Delivery) it will give that Delivery.Charge is NaN !!!
Moreover if we type console.log(Delivery.Charge) it will give 527 !
What is going on? Please, explain!
UPDATE
If even after GET method I type Delivery.Charge = 123 anyway console.log(Delivery) gives me Delivery.Charge as NaN. Crazy!
Change your property name to anything but "Charge".
For instance, Charge -> DeliveryCharge:
Delivery {
Id: 1,
ShippingDateTime: null,
ApproximateHandoverDateTime: null,
DeliveryCharge: 527,
Cost: null
}
The correct format of the object should be:
var Delivery = {
key:value,
key:value
};
I think Delivery is an object and you can not print it out directly.
You have to write a method that the same toString() in Java.
See here for example.
var Delivery = {
Id: 1,
ShippingDateTime: null,
ApproximateHandoverDateTime: null,
Charge: 527,
Cost: null
}
function test(){
alert(JSON.stringify(Delivery)) //you use stringify to get all elements of oject.
}
Related
So I have this instructions:
Schema Defs:
Result object:
It's a map of string keys and number values
"result": { "M": { [STRING]: { "N": "401" } },
This is what I have so far
result: {
type: Object,
schema: {
// I am getting 2 errors:
// Parsing error: Unexpected token, expected "]"
// ',' expected.
[id: String]: Number
},
required: true
},
Any ideas?
[id: String] is a TypeScript thing. Not allowed in standard JavaScript.
This is not technically possible in Dynamoose. The only option here is to use the saveUnknown schema setting.
This was brought up in a conversation before, and the user who wanted to do this I told to create an issue on the GitHub repo but it doesn't look like that happened. If you'd like support for this in Dynamoose in the future, please submit a feature request on the GitHub repo.
Edit
In order to do this your schema would looks something like:
new dynamoose.Schema({
result: {
type: Object,
required: true
},
}, {
"saveUnknown": ["result.**"]
});
This will allow indefinitely nested objects within result.
Hi I'm trying to access the value DepartmentId but getting error as value doesn't exists.
I can get the values inside the curly braces when I try to access the DepartmentId, I'm getting error.
Below is my map function
r.PrimarySearchResults.map((value) => {
console.log(value)}
)
I can get value.Rank but not value.DepartmentId
Below is the JSON object I got. This is when I expand the Object.
{Rank: "16.9111518859863", DocId:
"17598046715456", Title: "HubSite", SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2",
WebTemplate: "SITEPAGEPUBLISHING", …}
Culture: "en-US"
DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}"
DocId: "17598046715456"
PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7"
Rank: "16.9111518859863"
RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js"
ResultTypeId: "0"
SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2"
SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada"
Title: "HubSite"
UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}"
UrlZone: "0"
WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a"
WebTemplate: "SITEPAGEPUBLISHING"
__proto__: Object
Find the below image
Tried arranging your JSON object, seems that DepartmentId is not included in the JSON
{
Rank: "16.9111518859863",
DocId: "17598046715456",
Title: "HubSite",
SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2",
WebTemplate: "SITEPAGEPUBLISHING", }
Culture: "en-US" DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}" DocId: "17598046715456" PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7" Rank: "16.9111518859863" RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js" ResultTypeId: "0" SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2" SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada" Title: "HubSite" UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}" UrlZone: "0" WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a" WebTemplate: "SITEPAGEPUBLISHING"
Try this:
var value = JSON.parse(value);
console.log(value.DepartmentId);
As mentioned above, the full JSON object being returned should be able to be parsed into an object and then gotten.
If your response object is PrimarySearchResults, you should be able to do:
r.PrimarySearchResults.DepartmentId;
// should return {3d408bfe-9172-4df5-b36e-863c066e9ada}
Im completely lost. This is some test code I use to print a specific key of an object, then im printing the entire object.
console.log(docs[0].mc_ign);
console.log(docs[0]);
Now this is the output I see on the console:
The__TxT
{
id: 0,
status: 1,
testing: false,
_id: 5dbc17eb20b3a8594d569570,
timestamp: 2019-11-01T11:32:59.380Z,
mc_uuid: 'dac89e44d1024f3b810478ed62d209a1',
discord_id: '653029505457586176',
email_address: 'gut97930#eveav.com',
country: 'Germany',
birth_month: 3,
birth_year: 1943,
about_me: 'about me text',
motivation: 'motivation text',
build_images: '',
publish_about_me: true,
publish_age: false,
publish_country: true,
__v: 0
}
Where is the mc_ign key?
The object itself comes from mongoose, the missing key is added by me after the fact:
docs[i].mc_ign = mc_ign;
I tried logging the entire object before and after I add the key and assign the value. They are both the same.
What am I missing? Why can I read the value out, but cant see it?
It is mongoose document object. To achieve what you want do the following.
docs[0] = docs[0].toObject();
docs[0].mc_ign = "stuff";
console.log(docs[0])
.toObject() convert it to plain JS object.
I am trying to do a validation with Joi, but I came to a scenario that I cannot achieve. I have an object. In some cases, the object will have an id (edit) and on the others it will not (create).
I want to validate that in case "id" is not present, few other fields should be there.
So far my only solution that works is when id is null and not missing at all. Is there a way to match non-existing key or should I change it to null (a bit hacky).
joi.object().keys({
id: joi
.number()
.min(0)
.allow(null),
someOtherField1: joi.when("id", { is: null, then: joi.required() })
});
Thank you in advance!
Undefined is the empty value for Joi. Use "not" instead of "is"
Example: { not: Joi.exist(), then: Joi.required() }
If you're prepared to accept null as an id and would like to perform conditionals on it I'd suggest defaulting the id field to null. This way you can omit it from the payload and still query it using Joi.when().
For example:
Joi.object().keys({
id: Joi.number().min(0).allow(null).default(null),
someOtherField1: Joi.string().when('id', { is: null, then: Joi.required() })
});
Passing both an empty object, {}, and an object with a null id, { "id": null }, will result in:
ValidationError: child "someOtherField1" fails because ["someOtherField1" is required]
I am trying to push some things onto a Mongoose model. The model looks like this.
var ScheduleSchema = new mongoose.Schema({
hours: Number,
items: [{number: Number, minutes: Number, details: {description: String}, type: String}],
userId: Number
});
//later
ScheduleSchema.methods.createNew = function(hours, tasks, breathers) {
var schedule = makeSchedule(hours, tasks, breathers);
console.log(schedule);
this.items = schedule;
console.log(this.items);
}
I think that is enough code for my issue, but I can give more code if needed. Essentially, I've got a method for creating a schedule and then I want to assign the schedule to the object's 'items' property. I must admit I am still learning about mongoose so it is likely a problem there.
Anyway, I know my makeSchedule function is working, because I see this as the output from the first console message.
[{ number: 1,
minutes: 30,
details: {description: 'Task A'},
type: 'task'},
{ number: 2,
minutes: 45,
details: {description: 'Task B'},
type: 'task'},
etc...
]
However, when the console output from my second log statement, this.items, prints, I don't see the same structure. Instead, I see
["[object Object]", "[object Object]", "[object Object]", etc...]
Why am I not able to just assign the schedule variable to this.items? I believe I was even able to do it before, but I made some changes to my schedule code and now I cannot.
That would lead me to believe that the error is in my schedule code, but as you can see, it is creating the list of items just fine, based on the console output. Can anyone see a really obvious, potentially mongoose related mistake that I may have missed as a rookie?
My guess is that you recently added the type field to the embedded objects in items which is making Mongoose now think that items contains an array of strings instead of an array of objects like you have.
To fix it, use an object to define the type for type in your schema like so:
var ScheduleSchema = new mongoose.Schema({
hours: Number,
items: [{
number: Number,
minutes: Number,
details: {description: String},
type: {type: String}
}],
userId: Number
});