因此,我希望返回数据库结果,而不想返回其他结果。目前,我得到了大量的JSON数据(如下所示):
但我只需要dataValues属性。我不想使用JSON的这一部分来检索它:tagData[0].dataValues.tagId。
我刚刚注意到:当它找到并且没有创建时,它会返回数据库结果的JSON,但是当它没有找到并创建时,它会返回不需要的JSON blob (如下所示)。有什么方法可以解决这个问题吗?
[ { dataValues:
     { tagId: 1,
       tagName: '#hash',
       updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
       createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
    _previousDataValues:
     { tagId: 1,
       tagName: '#hash',
       createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
       updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
    _changed:
     { tagId: false,
       tagName: false,
       createdAt: false,
       updatedAt: false },
    '$modelOptions':
     { timestamps: true,
       instanceMethods: {},
       classMethods: {},
       validate: {},
       freezeTableName: true,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       whereCollection: [Object],
       schema: null,
       schemaDelimiter: '',
       defaultScope: null,
       scopes: [],
       hooks: {},
       indexes: [],
       name: [Object],
       omitNull: false,
       sequelize: [Object],
       uniqueKeys: [Object],
       hasPrimaryKeys: true },
    '$options':
     { isNewRecord: true,
       '$schema': null,
       '$schemaDelimiter': '',
       attributes: undefined,
       include: undefined,
       raw: true,
       silent: undefined },
    hasPrimaryKeys: true,
    __eagerlyLoadedAssociations: [],
    isNewRecord: false },
  true ]我只需要RAW json结果(如下所示),而不是像上面这样得到大的斑点:
{ tagId: 1,
       tagName: '#hash',
       updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
       createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },我使用了下面的javascript。我确实试过添加raw: true,但它不起作用?
    // Find or create new tag (hashtag), then insert it into DB with photoId relation
module.exports = function(tag, photoId) {
    tags.findOrCreate( { 
        where: { tagName: tag },
        raw: true
    })
    .then(function(tagData){
        // console.log("----------------> ", tagData[0].dataValues.tagId);
        console.log(tagData);
        tagsRelation.create({ tagId: tagData[0].dataValues.tagId, photoId: photoId })
        .then(function(hashtag){
            // console.log("\nHashtag has been inserted into DB: ", hashtag);
        }).catch(function(err){
            console.log("\nError inserting tags and relation: ", err);
        });
    }).catch(function(err){
        if(err){
            console.log(err);
        }
    });
}编辑:
所以我做了一些调查,似乎只有当Sequelize正在创建而没有找到时,才会返回大的JSON blob。
有没有办法绕过这个问题?
编辑2:
好的,我找到了一个变通方法,它可以变成一个可重用的函数。但如果Sequelize中内置了什么,我更喜欢使用它。
var tagId = "";
// Extract tagId from json blob
if(tagData[0].hasOwnProperty('dataValues')){
    console.log("1");
    tagId = tagData[0].dataValues.tagId;
} else {
    console.log("2");
    console.log(tagData);
    tagId = tagData[0].tagId;
}
console.log(tagId);
tagsRelation.create({ tagId: tagId, photoId: photoId })编辑3:
因此,我不认为有一种“官方”的顺序化方法来实现这一点,所以我只是编写了一个自定义模块来返回所需的JSON数据。这个模块可以定制和扩展,以适应各种情况!如果任何人对如何改进该模块有任何建议,请随时发表评论:)
在这个模块中,我们返回一个Javascript对象。如果您想将其转换为JSON,只需使用JSON.stringify(data)将其串化即可。
// Pass in your sequelize JSON object
module.exports = function(json){ 
    var returnedJson = []; // This will be the object we return
    json = JSON.parse(json);
    // Extract the JSON we need 
    if(json[0].hasOwnProperty('dataValues')){
        console.log("HI: " + json[0].dataValues);
        returnedJson = json[0].dataValues; // This must be an INSERT...so dig deeper into the JSON object
    } else {
        console.log(json[0]);
        returnedJson = json[0]; // This is a find...so the JSON exists here
    }
    return returnedJson; // Finally return the json object so it can be used
}编辑4:
所以有一个官方的sequelize方法。请参考下面接受的答案。
https://stackoverflow.com/questions/34460482
复制相似问题