How to verify that Collections.insert () is successfully inserted or not in Meteor?

How to check (user-created collections) The Collections.insert () command was inserted successfully or not in Meteor JS?. For example, I use Client Collections to insert information, as shown below:

Client.insert({ name: "xyz", userid: "1", care:"health" });

How do I know if the above insert request is successfully inserted or not ?. Due to below problem

 If the form details are successfully inserted  - do one action
  else -another action

So please suggest me what to do?

+4
source share
2 answers

The insert provides the server response in the arguments to the callback function. It provides two arguments, “error” and “result,” but one of them will always be zero, depending on whether the insert is successful.

Client.insert( { name: "xyz", userid: "1", care:"health" }
  , function( error, result) { 
    if ( error ) console.log ( error ); //info about what went wrong
    if ( result ) console.log ( result ); //the _id of new object if successful
  }
);

See more details.

+5

user728291, , :

var value = Collection.insert({foo: bar});

_id ( , db ). try...catch, , :)

, :

try {
    var inserted = Collection.insert({foo: bar});
} 
catch (error) {
    console.log("Could not insert due to " + error);
}

if (inserted)
    console.log("The inserted record has _id: " + inserted);

@user728291 .

+4

Source: https://habr.com/ru/post/1526117/


All Articles