2016-06-21 14 views
0

Ich habe zwei Sammlungen; Benutzer und Adresse. Die Adresssammlung ist mit der Benutzersammlung verknüpft.Sailsjs Update-Modell Mongodb

module.exports = { 
    attributes: { 
    email: { 
    type: 'email', 
    required: true, 
    unique: true 
    }, 
    cart: { 
     collection: 'cart', 
     via: 'user' 
    }, 
    isAdmin : { 
     type : 'boolean', 
     defaultsTo : false 
    }, 
} 

module.exports = { 
attributes: { 
    user: { 
     model: 'user', 
     required: true 
    }, 
    address: { 
     type: 'string', 
    }, 
    addressAdditional: { 
     type: 'string', 
    }, 
    city: { 
     type: 'string', 
    }, 
    state: { 
     type: 'json', 
    }, 
    zip: { 
     type: 'string', 
    }, 
} 

Ich habe die userId und Adressinformationen von der Benutzeroberfläche übergeben und ich möchte die Adresssammlung aktualisieren. Ich habe das folgende Snippet ausprobiert, aber es scheint nicht so, als ob die Daten aktualisiert werden.

var address = { 
    address: "123", 
    city: "Test City", 
    state: "NY", 
    zip: "12345" 
}; 

User.findOne({user:userId}).then(function(model){ 
Address.update({id:model.id}, address)}); 

Was mache ich falsch? Danke

+0

Ist das Feld 'id' eines Dokuments in der Adresssammlung ein 'ObjectId' Typ? Wenn ja, wandle diese ID in eine 'ObjectId' um. – Kevin

Antwort

0

Nach dem Blick in die Segel adapter.js Javascript, eine Funktion namens mongo.objectId, um eine ObjectId zu erstellen.

var addressObjectId = mongo.objectId(model.id); 
Address.update({_id:addressObjectId}, address)}) 

https://github.com/balderdashy/sails-mongo/blob/master/lib/adapter.js#L266

+0

Ich konnte nicht herausfinden, wie adapter.js zu verwenden, stattdessen nahm ich den Ansatz in http://stackoverflow.com/questions/25699097/native-update-in-sails-with-mongo-does-not-work -mit-Objekt-ID – gamegeek24