Trying to update the database using the strapi.db.query even providing data, failed with "Update required data object" - strapi

Below is the code that I tried to update the database using the id,
try {
await strapi.db.query("api::otp.otp").update(otpSent.id, {
where: { otpSent },
data,
});
} catch (error) {
console.log(error);
}
but I am stuck at the error saying
Error: Update requires a data object.
I have checked the entity-manager.js to see arguments are correct. But, at last I do not get any idea to update the query. Please give some tips to me to figure this out. The only thing is to update the database entries by id in the strapi controller.

Actually, I should use
strapi.service("api::otp.otp").update(otpSent.id, { data: data })

Related

Updating Multiplb Objects/Rows in Parse Dashboard

Im new to parse.com. Is it possible to update multiple objects/rows in Parse dashboard? Something like running update query in dashboard?
Parse.Object.update({...some filter...}, {...some values...})
Thanks
There's no SQL-like UPDATE syntax. You would need to query for and iterate through the results, making the changes to each object. The each method is provided on a Parse.Query for processing every record that matches:
var query = new Parse.Query("MyClass");
query.equalTo("someField", "someValue");
query.each(function(obj) {
obj.set("otherField", "otherValue");
return obj.save();
}).then(function() {
// All objects updated.
}, function(err) {
console.log(err);
});

Update or insert new parse object using cloud code

As many who came before me, I'm trying to run a bit of cloud code that will check for uniqueness and then insert or update the object as necessary.
The code correctly determines whether or not there is an existing object in the db with the same device token as the request.object
However, the existing object will not update it's countdownValue to 200.
I have tried adding and omitting the object.save() function
I have tried adding, omitting, and exchanging the response.error and response.success functions
The preexisting object remains untouched in all cases.
I have tried Updating existing Parse object in Cloud Code and many others.
Any help or thoughts would be greatly appreciated.
var Countdown = Parse.Object.extend("Countdown");
Parse.Cloud.beforeSave("Countdown", function(request, response) {
if (!request.object.get("devicetoken")) {
response.error('A Countdown must have a devicetoken.');
} else {
var query = new Parse.Query(Countdown);
query.equalTo("devicetoken", request.object.get("devicetoken"));
query.first({
success: function(object) {
if (object) {
object.set("countdownValue", "200");
object.save();
response.error("Failing on purpose");
}
else
{
response.success();
}
},
error: function(error) {
response.error("Could not validate uniqueness for this Countdown object.");
}
});
}
});

ElasticSearch real time GET API doesn't seem to work

I'm trying to access a doc using the GET API of ElasticSearch but eventhough the documentation claims to be real time I cannot seem to make it work.
Here's what I tried:
Indexing an event with a custom id:
POST: http://hostname.com:9200/events/purchase/<custom_id>
Immediatedly retrieving the doc using:
GET: http://hostname.com:9200/events/purchase/<custom_id>
The problem is that the document is not found.
UPDATE:
It seems that the problem only occurs if the index is initially empty and that's the first doc to be written. Subsequent requests are indexed and retrieved just fine.
this might not be the best code but I think this shows what you want. If possible switch to the elasticsearch.js client. That is what is used in this sample code.
var elasticsearch = require('elasticsearch');
var config = {host:'localhost:9200'};
var client = new elasticsearch.Client({
host: config.host,
log:'trace'
});
storeEvent({"title":"My Event"}, 1);
function storeEvent(myEvent, id) {
client.create({
"index":"events",
"type":"purchase",
"id":id,
"body":myEvent
}, function(error,response) {
if (error) {
console.log("Error during creating event");
console.log(error);
} else {
console.log("Submitted event");
}
client.get({
"index":"events",
"type":"purchase",
"id":id
}, function (error,response) {
if (error) {
console.log("Error during obtaining event");
console.log(error);
} else {
console.log(response);
}
})
});
}
Which version are you using? 1.2.0 had a bug involving routing: http://www.elasticsearch.org/blog/elasticsearch-1-2-1-released/
If you fixed things by reindexing your data it's worth noting that it won't be compatible with future versions.

Parse.User.save() doesn't work with Facebook API

I'm using the Facebook SDK to auth a user, and trying to save the user's email to the record after authenticating. However, I keep getting an error on the save call.
The code in question:
Parse.FacebookUtils.logIn({
access_token: authResponse.access_token,
expiration_date: expire_date.toISOString(),
id: response.id
},
{
success: function(user) {
console.log("success!");
user.set({"email":response.email});
user.save();
window.App.navigate("#myplaces", {trigger:true});
},
...
That user.save() call returns error occurred: http://www.parsecdn.com/js/parse-1.1.14.min.js:1: TypeError: 'undefined' is not an object.
According to the docs, I have to be in an authentication call (".logIn", etc.) to perform save(), so I'm wondering if this still works with Parse.FacebookUtils.logIn. Seems like it should.
Ideas as to why this isn't working? The ideal behavior is to log the user in, retrieve information from the FB response, and save that back to the user record on Parse.
Thanks!
Justin
Not sure about this but I had the same problem in Cloud Code and I used success/error callbacks when calling save(...):
user.save(null, {
success: function(){
// Code
},
error: function(){
// Code
}
});
See also here: https://parse.com/questions/saving-a-relation-on-the-current-user

Node.js Express mongoose query find

I have a little problem with Express and mongoose using Node.js . I pasted the code in pastebin, for a better visibility.
Here is the app.js: http://pastebin.com/FRAFzvjR
Here is the routes/index.js: http://pastebin.com/gDgBXSy6
Since the db.js isn't big, I post it here:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
module.exports = function () {
mongoose.connect('mongodb://localhost/test',
function(err) {
if (err) { throw err; }
}
);
};
var User = new Schema({
username: {type: String, index: { unique: true }},
mdp: String
});
module.exports = mongoose.model('User', User);
As you can see, I used the console.log to debug my app, and I found that, in routes/index.js, only the a appeared. That's weird, it's as if the script stopped (or continue without any response) when
userModel.findOne({username: req.body.username}, function(err, data)
is tried.
Any idea?
You never connect to your database. Your connect method is within the db.export, but that is never called as a function from your app.
Also, you are overwriting your module.exports - if you want multiple functions/classes to be exported, you must add them as different properties of the module.export object. ie.:
module.export.truthy = function() { return true; }
module.export.falsy = function() { return false; }
When you then require that module, you must call the function (trueFalse.truthy();) in order to get the value. Since you never execute the function to connect to your database, you are not recieveing any data.
A couple of things real quick.
Make sure you're on the latest mongoose (2.5.3). Update your package.json and run npm update.
Try doing a console.log(augments) before your if (err). It's possible that an error is happening.
Are you sure you're really connecting to the database? Try explicitly connecting at the top of your file (just for testing) mongoose.connect('mongodb://localhost/my_database');
I'll update if I get any other ideas.

Resources