Coder Social home page Coder Social logo

Comments (6)

raymondfeng avatar raymondfeng commented on June 14, 2024

Can you provide a sample for saveEventObj? What error message are you seeing?

On Feb 4, 2014, at 8:30 AM, Sean Brookes [email protected] wrote:

I'm unable to update an existing document in my mongodb.

My model code looks like this:

"event": {
"properties": {
"date": {
"type": "date"
},
"type": {
"type": "string"
},
"nearestCity": {
"type": "string"
},
"stateProv": {
"type": "string"
},
"country": {
"type": "string"
},
"blurb": {
"type": "string"
},
"notes": {
"type": "string"
},
"createdDate": {
"type": "date"
},
"lastUpdate": {
"type": "date"
},
"material": {
"type": "string"
},
"tags": {
"type": "array"
}
},
"public": true,
"dataSource": "mongodb",
"plural": "events"
},
I'm also using the loopback-angular auto-generated lb-services code and calling:

EventService.api.Event.upsert(saveEventObj,
function(response){
console.log('Saved the event: ' + JSON.stringify(response));
},
function(response){
console.log('bad update event: ' + JSON.stringify(response));
}
);

Reply to this email directly or view it on GitHub.

from loopback-connector-mongodb.

seanbrookes avatar seanbrookes commented on June 14, 2024

sorry, hit send before I was done writing

from loopback-connector-mongodb.

seanbrookes avatar seanbrookes commented on June 14, 2024

for clarity, here is the saveEventObj instance:

{
"date":"2013-10-17T07:00:00.000Z",
"type":"train",
"nearestCity":"Sexsmith",
"stateProv":"Alberta",
"country":"Canada",
"blurb":"<p> Residents in the northwestern Alberta town of Sexsmith were forced from their homes after four CN rail cars carrying anhydrous ammonia left the rails. The cars remained upright and there were no leaks.</p>",
"notes":null,
"createdDate":null,
"lastUpdate":1391532380517,
"material":null,
"tags":[],
"id":"52e8a43c36354025bd0c1144",
"_id":"52e8a43c36354025bd0c1144",
"$$hashKey":"006"
}

from loopback-connector-mongodb.

raymondfeng avatar raymondfeng commented on June 14, 2024

Can you try delete _id before save? We can do that on the server side for update if it works.

Thanks,
Raymond

On Feb 4, 2014, at 8:47 AM, Sean Brookes [email protected] wrote:

for clarity, here is the saveEventObj instance:

{
"date":"2013-10-17T07:00:00.000Z",
"type":"train",
"nearestCity":"Sexsmith",
"stateProv":"Alberta",
"country":"Canada",
"blurb":"

Residents in the northwestern Alberta town of Sexsmith were forced from their homes after four CN rail cars carrying anhydrous ammonia left the rails. The cars remained upright and there were no leaks.

",
"notes":null,
"createdDate":null,
"lastUpdate":1391532380517,
"material":null,
"tags":[],
"id":"52e8a43c36354025bd0c1144",
"_id":"52e8a43c36354025bd0c1144",
"$$hashKey":"006"
}

Reply to this email directly or view it on GitHub.

from loopback-connector-mongodb.

seanbrookes avatar seanbrookes commented on June 14, 2024

as mentioned in a follow up edit I did try deleting the '_id' property from
the model instance before saving it but it ended up generating a copy of
the model in mongodb with null for the id and _id properties

On Tue, Feb 4, 2014 at 9:29 AM, Raymond Feng [email protected]:

Can you try delete _id before save? We can do that on the server side for
update if it works.

Thanks,
Raymond

On Feb 4, 2014, at 8:47 AM, Sean Brookes [email protected]
wrote:

for clarity, here is the saveEventObj instance:

{
"date":"2013-10-17T07:00:00.000Z",
"type":"train",
"nearestCity":"Sexsmith",
"stateProv":"Alberta",
"country":"Canada",
"blurb":"

Residents in the northwestern Alberta town of Sexsmith were
forced from their homes after four CN rail cars carrying anhydrous ammonia
left the rails. The cars remained upright and there were no leaks.

",
"notes":null,
"createdDate":null,
"lastUpdate":1391532380517,
"material":null,
"tags":[],
"id":"52e8a43c36354025bd0c1144",
"_id":"52e8a43c36354025bd0c1144",
"$$hashKey":"006"

}

Reply to this email directly or view it on GitHub.

Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-34084516
.

from loopback-connector-mongodb.

seanbrookes avatar seanbrookes commented on June 14, 2024

I was able to make it work with the following:

  • make sure 'id' is setup as an explicit property on the model definition with id set to true (it was implicit in my initial model def)
    eg:
  "event": {
    "properties": {
      "id": {
        "type": "string",
        "id":"true"
      },
      "date": {
        "type": "date"
      },
...
    }
  • make sure to delete the '_id' property on the object before submitting:
            saveEventObj.lastUpdate = Date.now();

            delete saveEventObj._id;
            Event.upsert(saveEventObj,
              function(response){
                console.log('Saved the event: ' + JSON.stringify(response));
              },
              function(response){
                console.log('bad update event: ' + JSON.stringify(response));
              }
            );
  • use 'upsert' instead of 'save' as save does a 'POST' isntead of a 'PUT'

from loopback-connector-mongodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.