2017-05-31 33 views
1

我正在嘗試使用$ set更新MongoDB集合。集合中的字段不存在。當我嘗試添加字段時,集合會短暫存儲數據,然後數據將消失並返回錯誤ClientError: name.first is not allowed by the schema。我不知道我在這裏做錯了什麼,我一直在谷歌搜索幾個小時。簡單架構不會更新

我使用:

  • 流星
  • 簡單模式(NPM)
  • 流星collection2核心

路徑:Simple-Schema

const ProfileCandidateSchema = new SimpleSchema({ 
    userId: { 
    type: String, 
    regEx: SimpleSchema.RegEx.Id, 
    }, 
    createdAt: { 
    type: Date, 
    }, 
    name: { type: Object, optional: true }, 
    'name.first': { type: String }, 
}); 

路徑:Method.js

import { Meteor } from 'meteor/meteor'; 
import { ProfileCandidate } from '../profileCandidate'; 
import SimpleSchema from 'simpl-schema'; 
import { ValidatedMethod } from 'meteor/mdg:validated-method'; 

export const updateContactDetails = new ValidatedMethod({ 
    name: 'profileCandidate.updateContactDetails', 

validate: new SimpleSchema({ 
    'name.first': { type: String }, 
    }).validator({ clean: true }), 

run(data) { 
    ProfileCandidate.update({userId: this.userId}, 
    {$set: 
     { 
     name: {'first': "Frank"}, 
     } 
    }, (error, result) => { 

    if(error) { 
     console.log("error: ", error) 
    } 
}); 
} 
}); 

UPDATE

路徑:call function

updateContactDetails.call(data, (error) => { 
    if (error) { 
    console.log("err: ", error); 
      console.log("err.error: ", error.error); 
    } 
}); 

回答

1

你能嘗試更換:

validate: new SimpleSchema({ 
    'name.first': { type: String }, 
}).validator({ clean: true }), 

在Method.js由:

validate: new SimpleSchema({ 
    name: { 
    type: new SimpleSchema({ 
     first: String, 
    }), 
    optional: true, 
}).validator({ clean: true }), 
+0

這沒有奏效。錯誤'調用方法時出現異常'profileCandidate.updateContactDetails'模式不允許name.first。在Array.forEach'。我添加上面的ProfileCandidate.call。 – bp123

+0

這是你想要的嗎? – bp123

+1

這似乎工作。我真的不理解驗證器功能。 – bp123