Storing Json Object in Mongoose String key

前端 未结 5 808
温柔的废话
温柔的废话 2021-01-30 04:14

In my Mongoose schema, I have a field which is a String, and I want to be able to store an JSON object in it. Is it possible? In Postgres, it\'s possible to store a dictionary i

5条回答
  •  花落未央
    2021-01-30 04:52

    Couldn't alter the original due to the 6 change limit on stack-overflow. re-posted, awesome work Tom, was just missing catch(err) in the catch block

    data: {
      type: String,
      get: function(data) {
        try { 
          return JSON.parse(data);
        } catch(err) { 
          return data;
        }
      },
      set: function(data) {
        return JSON.stringify(data);
      }
    }
    

提交回复
热议问题