I sent this request which is an array of objects from reactjs app to nodejs(sequelize,mysql)backend
{
"Qusetion":"new question",
"Qusetiontype":"AddedAnswers",
"Qusetionanswers":[
{
"QuestionAnswer":"a1"
},
{
"QuestionAnswer":"a2"
}
],
"Surveyid":"9fe96b40-7704-11e9-b46f-3f2c20a71682"
}
the nodejs script
QuestionAnswer.create(req.body.Qusetionanswers.map(Answer=>{
Qusetionanswer=Answer.QuestionAnswer,
QuestionId=qid
})
).then(res=>{console.log(res)}).catch(err=>{console.log(err)})
My Problem: I got below error msg in postman when trying to send new question I searched and found that create not accept an array of objects and nothing else.so any help thanks in advance
{
"message": "this.build(...).save is not a function"
}
I would try it likes this. Creating one after one. Or are you trying to archive anything else?
req.body.Questionanswers.map(answer => {
QuestionAnswer.create({
Qusetionanswer: answer.QuestionAnswer,
QuestionId: qid
}).then(res => {console.log(res);}).catch(err => {console.log(err);}
);
});
Related
I'm using the Wordpress API to update tribe events. The updates are successfully sent (and the change is made) but as a side-effect, any time I do it, the featured image is removed from the post...
wpapi package setup:
Because The Events Calendar has it's own routes that aren't built-in by default in the wpapi package, I use the wp.registerRoute function to set that up. I don't think there's a problem with the way I set that up because otherwise it works.
var WPAPI = require( 'wpapi' );
var wp = new WPAPI({
endpoint: 'https://www.example.com/wp-json',
username: 'username',
password: 'mypassword'
});
wp.tribeevent = wp.registerRoute('tribe/events/v1', '/events/(?P<id>)');
example update:
function showEvent (post_id) {
let data = { "hide_from_listings" : false }
wp.tribeevent().id(post_id).update(data)
}
Haven't been able to find anyone with this issue online, so I'm posting here after exhausting myself trying to get it to work without removing the image... Am I doing this wrong? Is this just a bug? Any suggested workarounds?
I've also tried adding the existing image info into the update data sent, but I get this response when doing so:
{
"error": {
"code": "rest_invalid_param",
"message": "Invalid parameter(s): image",
"data": {
"status": 400,
"params": {
"image": "Invalid parameter."
},
"details": []
}
}
}
Otherwise, when making an update such as { "hide_from_listings" : false }, when the json response comes back, the value of the image key just comes back as false: { "image" : false }
Would greatly appreciate any kind of input on this issue...
Recently, I changed key of object in MongoDB.
{
links: {
changed: 'value',
notchanged: 'value'
}
}
This is what I get from my MongoDB collection. Data which key is not changed is still readable by links.notchanged but data which key is changed like links.changed is not readable and only outputs undefined. Node.js gets and reads the whole links data correctly but when it comes to links.changed it doesn't. How do I solve this problem? Code below:
scheme.findOne({}, (err, data) => {
if (err) res.send('ERR')
else {
console.log(data) // prints full data, same as JSON above
console.log(data.links.changed) // undefined
}
}
You are matching {class:'210'}.. Is it available in document. Probably Your query returns empty object in data . Confirm the match query... Otherwise your code seems ok.
await db1.findOne({class: "210"}, (err, data) => {
console.log(data.links.changed) // returns value
})
Or Try the code like this
await db1.find({ class: "210" }).toArray()
.then(data => {
console.log(data[0].links.changed) //"value"
});
You should make a variable instead of an object for this. For example: Use changed and assign it value as true or false
i'm writing a get methode in node js and i have an error when i want get the data base it show me an error that the query return null despite the document exist
router.get('/getmodele',(req,res,next)=>{
let aa=req.query.imei;
console.log(aa);
Post.findOne( {imei: {
'imei.name':req.query.imei,'imei.modele':req.query.modele
},
test: {
$exists: false
}})
.
then((posts) => {
my request take at time two parameteres
i need some helps and thank you
Use dot notation when querying.
Post.findOne( {'imei.name':req.query.imei,'imei.modele':req.query.modele})
I think your query should be like this:
{
imei: {
name: req.query.imei,
modele: req.query.modele
}
}
Hello I'm trying to access to an especific key from a JSON. I'm using node.js and angular 7
The JSON I got is stringfy and it comes from an API.
This is the JSON I got
"search": {
"entry": [
{
"dn": "uid=080030781,c=mx,ou=s,o=i.com",
"attribute": [
{ "name": "mail", "value": ["CAMILA.CAMPOS.TELLEZ#mail.com", "MX08#mail.com"] }]
}
],
"return": {
"code": 0,
"message": "Success",
"count": 1
}
}
}
I need to access to the key "value", because I need to get the value "camila.campos.tellez#mail.com".
I get the JSON from an API declarated in a node.js file called app.js, then I catch the response from it using this service.ts file
getApproverMail(name) {
console.log('entered to rootservice');
return this.http.get(this.baseUrl + '/costing/operations?name=' + name);
}
Finally I can access to this through a component.ts fil with this code
findApproverMail() {
this.rootService.getApproverMail(this.aproverName).subscribe((res) => {
this.email = res;
console.log('Test: ' + res);
});
}
And the browser console prints the JSON I have show you. But how can I access only to the mail's value?
P.D I need only the mail because after I got it the web site needs to send an email to that direction
JSON stands for JavaScript Object Notation. Any valid JSON is an object in JavaScript (and TypeScript). You can use dot notation to navigate into the object:
findApproverMail() {
this.rootService.getApproverMail(this.aproverName).subscribe((res) => {
this.email = res.search.entry[0].attribute[0].value[0];
console.log('Test: ' + res);
});
}
I would recommend defining an interface in the shape of what you expect from the service. It's cleaner than just indexing around an any typed object.
Also note that I hard code 0 indexes because that answers your question. You might consider a more dynamic / flexible way to get the address or elements you need.
I'm trying to keep a list of all the sites I'm working on right now, and I'm having issues using $push.
I've created a document with this:
accountData = {
'accountid': account_id,
sites: {
'001': 'example.com',
}
};
db.accounts.insert(accountData);
This works great, I get:
{ accountid: 'AC654164545', 'sites.001': { '$exists': true } }
And I would like to add to the sites object. This is what I'm trying:
db.accounts.update(
{'accountid': account_id},
{
$push:
{
sites:
{
'002': 'example2.com'
}
}
},
function(err,doc){
console.log(err);
}
);
The error that I get is:
err: 'The field \'sites\' must be an array but is of type Object in document
I don't want the document to be created with an array, as I know that if I did something like this when inserting:
sites: {
'002': 'example2.com',
'003': 'example3.com',
'004': 'example4.com',
}
It would work just fine.
How do I use $push, or any other command, to add to the "sites" object without it being an array?
It can't be an array because I'm using the following to search for existing sites.
search['sites.' + site_id] = { $exists : true };
Ok, I figured out what the problem was.
I wasn't thinking about the problem in the right context.
The error was correct in saying it needed to be an array, so I changed the "sites" object into an array like this when created (initial insert), so it's an array:
accountData = {
'accountid': account_id,
sites: [{'001': 'example.com'}]
};
Then, by using the exact same code above to $push, it worked properly.