I am trying to parse data. But this error comes:
Error parsing response
TypeError: Cannot read properties of undefined (reading 'response')
var result:
{
multistatus: {
'$': { xmlns: 'DAV:', 'xmlns:C': 'urn:ietf:params:xml:ns:caldav' },
response: [ [Object], [Object] ]
}
}
Code:
var data = result['D:multistatus']['D:response'];
if(data) {
data.map((event) => {
var ics = event['D:propstat'][0]['D:prop'][0]['C:calendar-data'][0];
var jcalData = ICAL.parse(ics);
var vcalendar = new ical.Component(jcalData);
var vevent = vcalendar.getFirstSubcomponent('vevent');
reslist.push(vevent)
});
}
Error:
Error parsing response
TypeError: Cannot read properties of undefined (reading 'response')
(Error to the line var data ...)
i expected to parse the data on correctly. Afterwards the events get filtered again.
You data structure is this:
{
multistatus: {
'$': { xmlns: 'DAV:', 'xmlns:C': 'urn:ietf:params:xml:ns:caldav' },
response: [ [Object], [Object] ]
}
}
Which means you should use the actual property names multistatus and response and change this:
var data = result['D:multistatus']['D:response'];
to this:
var data = result['multistatus']['response'];
I don't know why you put the D: at the front of the property names. Those are not present in the actual property names you show and must be removed.
Also, you should not be using var any more. Use either let or const.
The error message you get:
Cannot read properties of undefined (reading 'response')
Comes because this first part:
var data = result['D:multistatus']
results in undefined (since the D:multistatus property does not exist) and then you try to reference the second property off undefined, with this:
var data = result['D:multistatus']['D:response'];
you get that error as undefined is not an object and thus gives you an error when you try to reference a property on it.
Related
well, I am getting this error in the console when I submit the form.
TypeError: Cannot read property 'file' of null
I have a form where I need to upload text, files so the file field is optional? which means it can be null if the user does not want to upload a file, here is the code where the error happend.
the column name is this image1_url
writeFileToCloudinary(files.file).then((secureUrl) => {
Posts.update(
{ image1_url: secureUrl },
{
where: { id: postId },
}
);
});
any idea?
thanks
I am trying to insert a lot of data i database using Sequelize library
but it is showing some strange as I am passing an array of objects into the bulkCreate query .
I have function something like this
db.organizations
.findAll({
attributes: ['name'],
where: {
id: req.ORG_ID
}
})
.then(org => {
let i; let smsData = [];
for (i = 0; i < user.length; i++) {
let DefaultText = Utils.defaultMessage(
user[i].name,
user[i].dataValues.amount,
org[0].name
);
smsData.push({
organizationId:
user[i].dataValues.organizationEntries[0].organizationId,
mobile: user[i].mobile,
message: DefaultText,
status: 0
});
}
console.log(JSON.stringify(smsData));
db.logSms.bulkCreate([{
smsData
}]).then(() => { // Notice: There are no arguments here, as of right now you'll have to...
return db.logSms.findAll({ where: { status: 0 } });
}).then(sms => {
console.log(sms)
})
and I have also done console.log(smsData) which is showing array of objects but I am not able to understand why I am facing this error.
INSERT INTO log_sms (id,created_at,updated_at) VALUES (NULL,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);
Unhandled rejection SequelizeDatabaseError: Field 'mobile' doesn't have a default value
This error is basically coming as while executing the query as u can see below the error as I have made mobile field compulsory and I am passing the array of objects which is having mobile number but still this error is coming. Please give some hint
I have got my mistake as I should remove the braces from this statement:
db.logSms.bulkCreate([{ smsData }])
it should be like this:
db.logSms.bulkCreate(smsData)
How can i get JSON objects in the following case:
Here is my post request:
var articles = client.post("http://host:port/path", args, function (data, response) {
// console.log(xml.parseBuffer(data))
console.log(data.toString('utf8'));
// return xml.parseBuffer(data);
});
result of data.toString('utf8') look like:
<WebServiceResponse>
<page>
...
</page>
<articleDataList>
<articleId>
<idArticle>100000</idArticle>
<index>test</index>
</articleId>
<goodType>test</goodType>
<idAttributeSubject>100001</idAttributeSubject>
<identyfiable>false</identyfiable>
<isActive>true</isActive>
<isGoodSet>false</isGoodSet>
<longName>test</longName>
<translationSubjectId>
<idTranslationSubject>100408</idTranslationSubject>
</translationSubjectId>
<unitCode>szt</unitCode>
<vatRate>0.2300</vatRate>
</articleDataList>
<articleDataList>
...
</articleDataList>
<articleDataList>
...
and xml.parseBuffer(data) looks:
{ name: 'articleDataList', childs: [Object] },
I need put into articles objects with only: idArticle, index and shortName
Is there any easy way?
In this output:
{ name: 'articleDataList', childs: [Object] }
Object represents javascript object that you need to stringify like this: JSON.stringify(Object)
Or you should convert this whole Buffer to string first, and then convert to JSON or javascript object:
xml.parseString(data.toString('utf8'))
I have some troubles with a variable after doing some mongodb processing
...................
console.log("MIP : value :" + value._id );
console.log("MIP : page id :" + value._id.id );
....................
The logs show this
MIP : value :{ id: '346593403645', _id: 57a868497e07fcf75f27009c, __v: 0 }
MIP : **page id :undefined**
I am not understanding why the value of value._id.id is undefined
Could you help please
Seems like You keep serialized data in value._id.
So try:
if(typeof value._id == 'string') {
value._id = JSON.parse(value._id);
}
also keep in mind that when You want to dump the variable don't concatenate variable with string.
just do:
console.log("MIP : value :", value);
it will show You the variable as is:
- if it's buffer so will show that it's buffer
- if object so will output it as object
Seems like Your json is malformed.
So use dirty-json package:
var dJSON = require('dirty-json');
dJSON
.parse(value._id)
.then(function (result) {
console.log(result.id);
});
This is a piece of code from my main js file which i'm using to call a function in another js file(js.js)
var fields = "workaholic alcoholic insomniac";
var valid = require("./js");
Hacker.find({name: "arun"}, fields)//, function(err, hackers) {
.then(valid.myfun)
.catch(err => console.log(err,fields));
This is the js.js file which i'm referencing in the main js file
exports.myfun = function(hackers) {
console.log(hackers);
console.log(hackers.workaholic);
var workaholic = hackers.workaholic;
var alcoholic = hackers.alcoholic;
var insomniac = hackers.insomniac;
console.log(workaholic);
console.log(alcoholic);
console.log(insomniac);
show(workaholic,alcoholic,insomniac);
}
function show(a,b,c) {
console.log("Am I workaholic: "+a);
console.log("Am I alcoholic: "+b);
console.log("Am I insomniac: "+c);
}
When i execute the main js file, I notice that I'm getting my query response in an array.
C:\Users\Balajee\Desktop\project\Ultro>node hack
[ { fun: {},
workaholic: 'Yes',
alcoholic: 'No',
insomniac: 'Yes',
_id: 5706541ba3fe824c2f017680 } ]
When i try to set the value of these fields to variables, I'm getting "undefined"
_id: 5706541ba3fe824c2f017680 } ]
undefined
undefined
undefined
undefined
Am I workaholic: undefined
Am I alcoholic: undefined
Am I insomniac: undefined
So, how do i fetch the value of the field which is returned by the mongoose in an array? is it possible to return the query without array?
Use findOne() instead of find(). This returns a JSON object rather that array of JSON objects.
You can use findOne instead of find. findOne will return a single object that matches your query, instead of an array.
Hacker.findOne({name: "arun"}, fields)
I think you should do:
exports.myfun = function(err, hackers) {
// ...
}
because the callback takes err as first param